SpriteKit手柄控制器

3

我正在尝试在摇杆方向改变时执行一些玩家精灵动画。

我正在使用TheSneakyNarwhal的摇杆类,该类使用以下方法:

 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(向右移动),它就会不断从开头调用该方法,而不会实际播放完整的动画。
只有当我停止使用操纵杆才能播放整个动画。

为什么“比较相等”对于模拟摇杆不足够?您没有提供关于问题的足够信息。 - sangony
我已经修改了解释,希望这样更有意义。我在Sprite Kit和Xcode方面仍然很新。 - Julian Faul
1个回答

0

听起来你一直在不停地调用你的动画。这会阻止动画播放超过前几帧。

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


创建一个BOOL属性:
@property (nonatomic) BOOL animationRunning;

当您的摇杆值表明您希望玩家向右奔跑时,您调用动画方法:
[self runRightAnimation];

在动画运行之前,会检查BOOL变量以查看它是否已经在运行:
-(void)runRightAnimation {

    if(animationRunning == NO) {

        animationRunning = YES;

        // your animation code...
    }
}

记得在摇杆不再处于所需位置时,将animationRunning = NO;设置为停止动画。

您可以展示一下我提供的代码的例子吗?这样我会非常感激。我已经尝试过了,但是我觉得我还是做错了 :( - Julian Faul
太棒了,现在它可以工作了,非常感谢你。我现在知道我做错了什么,我没有添加@property (nonatomic) BOOL...。非常感谢您的专业建议。 - Julian Faul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接