Reputation: 49
I have an arrow sprite, and it is for aiming purposes in my Cocos2d game. Therefore, I want it to point to where the user touches the screen. How do I program the rotation of the sprite so it will rotate to the user's touch location? Thanks!
Upvotes: 0
Views: 2607
Reputation: 634
I haven't actually done this before, but I have adapted some of my code (that makes a enemy ship face the player ship) to what you need. Hopefully this is correct.
//rotate to face the touch
CGPoint diff = ccpSub(sprite.position, touch.position);
float angleRadians = atanf((float)diff.y / (float)diff.x);
float angleOffset = CC_DEGREES_TO_RADIANS(90);
if(diff.x < 0)
{
angleRadians += angleOffset;
}
else
{
angleRadians -= angleOffset;
}
PengOne's answer (cool name BTW) was great though and I am voting it up because you should make use of it.
Upvotes: 0
Reputation: 48398
These tutorials may be helpful:
http://www.raywenderlich.com/2343/how-to-drag-and-drop-sprites-with-cocos2d
Also, this question is asked (with code) and answered (with more code) here: Rotating Sprite with Touch - Cocos2d
Upvotes: 1