How to make a sprite rotate to a touch Cocos2d
I have an arrow sprite, and it is for aiming purposes in my Cocos2d game. Therefore, I want it to point to w开发者_开发问答here 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!
These tutorials may be helpful:
http://www.learn-cocos2d.com/knowledge-base/cocos2d-iphone-faq/learn-cocos2d-public-content/manual/cocos2d-general/14826-how-to-rotate-a-sprite-in-a-circular-motion/
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
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.
精彩评论