Take a look on this code :
Its not working for me because I am using bones, but for your game using 2d it probably will work.