Spritekit Joystick

I am trying to do some animations of my sprite with the player whenever I change the direction of the joystick.

I am using TheSneakyNarwhal drop in the Joystick class which uses the following methods:

 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 speed.x of the joystick is above 0 (moving to the right), it continues to call the method from the beginning and will not actually play the full animation.

Only when I stop using the joystick does it play all the animation.

+3


source to share


1 answer


It looks like you keep calling your animation over and over. This will prevent the animation from actually playing for the previous couple of frames.

Create the BOOL that you set when you first started the animation. Subsequent calls to the animation will check the BOOL state and determine if the animation is running. You can reset BOOL when the joystick is no longer pointing.


Create a BOOL property:

@property (nonatomic) BOOL animationRunning;

      



When your joystick values โ€‹โ€‹indicate that you want your player to work properly, you call the animation method:

[self runRightAnimation];

      

Before starting the animation, BOOL is checked to see if it will run:

-(void)runRightAnimation {

    if(animationRunning == NO) {

        animationRunning = YES;

        // your animation code...
    }
}

      

Remember to set animationRunning = NO;

and stop the animation when the joystick is no longer in the desired location.

0


source







All Articles