简体   繁体   English

Spritekit操纵杆

[英]Spritekit Joystick

I'm trying to execute some animations of my player sprite whenever I change the direction of the joystick. 每当我改变操纵杆的方向时,我都会尝试执行播放器精灵的一些动画。

I'm using TheSneakyNarwhal's drop in Joystick class which uses the following methods: 我正在使用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];
    }

However, whenever the velocity.x of the joystick is above 0 (moving right) it keeps calling the method from the beginning and won't actually play the full animation. 但是,只要操纵杆的velocity.x高于0(向右移动),它就会从头开始调用方法,并且实际上不会播放完整的动画。

Only when I stop using the joystick does it play the whole animation. 只有当我停止使用操纵杆时才会播放整个动画。

It sounds like you keep calling your animation over and over again. 听起来你一遍又一遍地调用你的动画。 This would prevent the animation from actually playing past the first couple of frames. 这将阻止动画实际播放前几帧。

Create a BOOL which you would set the first time the animation runs. 创建一个BOOL,您将在第一次运行动画时设置该BOOL。 Subsequent calls to the animation would check the BOOL's status and determine if the animation is already running. 对动画的后续调用将检查BOOL的状态并确定动画是否已在运行。 You can reset the BOOL once the joystick is no longer pointing right. 一旦操纵杆不再指向右侧,您可以重置BOOL。


Create a BOOL property: 创建BOOL属性:

@property (nonatomic) BOOL animationRunning;

When your joystick values indicate you want your player to run right you call the animation method: 当您的操纵杆值指示您希望您的播放器正确运行时,您可以调用动画方法:

[self runRightAnimation];

Before the animation is run, the BOOL is checked to see if it is already running: 在运行动画之前,检查BOOL以查看它是否已在运行:

-(void)runRightAnimation {

    if(animationRunning == NO) {

        animationRunning = YES;

        // your animation code...
    }
}

Remember to set your animationRunning = NO; 记得设置你的animationRunning = NO; and stop the animation when the joystick is no longer in the desired position. 当操纵杆不再位于所需位置时停止动画。

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

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