yourface1234
yourface1234

Reputation: 49

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 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

Answers (2)

Aaron Goselin
Aaron Goselin

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

Related Questions