user1445205
user1445205

Reputation:

Animate rotation of SKSpriteNode

I am attempting to rotate an SKSpriteNode to face the direction of a CGPoint. I have managed to do this like so:

CGPoint direction = rwNormalize(offset);

self.player.zRotation = atan2f(direction.y, direction.x);

How would I call this so that the SKSpriteNode will animate its rotation rather than it being instantaneous. Would it also be possible to keep the same speed of rotation in the animation no matter where the sprite should turn? Thanks in advance!

Upvotes: 0

Views: 796

Answers (1)

Andrey Gordeev
Andrey Gordeev

Reputation: 32459

Use SKAction:

CGPoint direction = rwNormalize(offset);
float angle = atan2f(direction.y, direction.x);
// Speed of rotation (radians per second)
float speed = 2.0;

float duration = angle > M_PI_2 ? angle/speed : (angle + M_PI_2)/speed;
[self.player runAction:
    [SKAction rotateToAngle:angle duration:duration]];

Upvotes: 1

Related Questions