简体   繁体   English

如何在[SKAction rotationBy:duration:]期间获取正确的zRotation

[英]How to get correct zRotation during [SKAction rotateBy:duration:]

I'm running a rotateBy action that's repeated forever for a node, and occasionally I want to get the node's current rotation. 我正在运行一个rotateBy操作,该操作将对节点永久重复,有时我想获取该节点的当前旋转。 But zRotation always return 0. How can I get the correct zRotation for this node? 但是zRotation始终返回0。如何获得此节点的正确zRotation?

Edit: so finally what I'm doing is creating a custom rotation action instead of using Sprite Kit's default action. 编辑:所以最后我要做的是创建自定义旋转动作,而不是使用Sprite Kit的默认动作。 A lot more complicated and hard to extend but it does what I want and can be encapsulated in each node's own class. 它要复杂得多且难以扩展,但是它可以满足我的要求,并且可以封装在每个节点自己的类中。

I believe the reason why you are getting 0 is because you are using animation. 我相信得到0的原因是因为您正在使用动画。 The properties of nodes undergoing animation are not updated at each step in the simulation. 正在进行动画的节点的属性不会在模拟的每个步骤中更新。 You will need to manually rotate the node in the update method or create a physics body and set the angular velocity. 您将需要在更新方法中手动旋转节点或创建物理物体并设置角速度。 Using real-time motion will allow you to constantly monitor the properties of you nodes at each step in the simulation. 使用实时运动将使您能够在仿真的每个步骤中不断监视节点的属性。

You can see an example of simulating rotation using real-time motion (albeit more complex) in my answer here . 您可以在此处的答案中看到使用实时运动模拟旋转的示例(尽管更为复杂)。

If you paste the following into a new project, you should see the zRotation property change over time. 如果将以下内容粘贴到新项目中,则应该看到zRotation属性随时间变化。 With an 180 degree/second rotational rate and 60 FPS, the zRotation changes by ~3 degrees per update. 在180度/秒的旋转速率和60 FPS的情况下,每次更新zRotation更改zRotation度。

@implementation GameScene {
    SKSpriteNode *sprite;
}

-(void)didMoveToView:(SKView *)view {        
    self.scaleMode = SKSceneScaleModeResizeFill;

    sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
    sprite.xScale = 0.5;
    sprite.yScale = 0.5;
    sprite.position = CGPointMake (CGRectGetMidX(view.frame),CGRectGetMidY(view.frame));

    SKAction *action = [SKAction rotateByAngle:M_PI duration:1];

    [sprite runAction:[SKAction repeatActionForever:action]];

    [self addChild:sprite];
}

-(void)didEvaluateActions {
    // Wrap at 360 degrees
    CGFloat angle = fmod(sprite.zRotation*180/M_PI, 360.0);
    NSLog(@"%g",angle);
}

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM