Animate rotation of SKSpriteNode

烈酒焚心 提交于 2019-12-11 16:37:08

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/24423622/animate-rotation-of-skspritenode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!