Spritekit操纵杆

时间:2015-07-28 05:26:13

标签: objective-c animation sprite-kit

每当我改变操纵杆的方向时,我都会尝试执行播放器精灵的一些动画。

我正在使用TheSneakyNarwhal's drop in Joystick class,它使用以下方法:

 if (joystick.velocity.x > 0)
    {
        [self walkRightAnim];
    }
    else if (joystick.x < 0)
    {
        [self walkLeftAnim];
    }
    if (joystick.velocity.y > 0)
    {
        [self walkUpAnim];
    }
    else if (joystick.velocity.y < 0)
    {
        [self walkDownAnim];
    }
    if (joystick.velocity.x == 0 && joystick.velocity.y == 0)
    {
        [self idleAnim];
    }

My [self walkRightAnim];

    - (void)walkRightAnim {

        NSLog(@"%f", self.joystick.velocity.x);

        SKTexture *run0 = [SKTexture textureWithImageNamed:@"right1.png"];
        SKTexture *run1 = [SKTexture textureWithImageNamed:@"right2.png"];
        SKTexture *run2 = [SKTexture textureWithImageNamed:@"right3.png"];
        SKAction *spin = [SKAction animateWithTextures:@[run0,run1,run2] timePerFrame:0.2 resize:YES restore:YES];
        SKAction *runForever = [SKAction repeatActionForever:run];
        [self.player runAction:runForever];
    }

然而,只要操纵杆的velocity.x高于0(向右移动),它就会一直从头开始调用方法,并且实际上不会播放完整的动画。

只有当我停止使用操纵杆时才会播放整个动画。

1 个答案:

答案 0 :(得分:0)

听起来你一遍又一遍地调用你的动画。这样可以防止动画实际播放过前几帧。

创建一个BOOL,您将在第一次运行动画时设置该BOOL。对动画的后续调用将检查BOOL的状态并确定动画是否已在运行。一旦操纵杆不再指向右侧,您就可以重置BOOL。

创建BOOL属性:

@property (nonatomic) BOOL animationRunning;

当您的操纵杆值指示您希望您的播放器正确运行时,请调用动画方法:

[self runRightAnimation];

在运行动画之前,检查BOOL以查看它是否已在运行:

-(void)runRightAnimation {

    if(animationRunning == NO) {

        animationRunning = YES;

        // your animation code...
    }
}

请记住设置animationRunning = NO;并在操纵杆不再位于所需位置时停止动画。

相关问题