繁体   English   中英

Spritekit操纵杆

[英]Spritekit Joystick

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

我正在使用TheSneakyNarwhal在Joystick类中的下降,它使用以下方法:

 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(向右移动),它就会从头开始调用方法,并且实际上不会播放完整的动画。

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

听起来你一遍又一遍地调用你的动画。 这将阻止动画实际播放前几帧。

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


创建BOOL属性:

@property (nonatomic) BOOL animationRunning;

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

[self runRightAnimation];

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

-(void)runRightAnimation {

    if(animationRunning == NO) {

        animationRunning = YES;

        // your animation code...
    }
}

记得设置你的animationRunning = NO; 当操纵杆不再位于所需位置时停止动画。

暂无
暂无

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

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