Spritekit游戏杆 [英] Spritekit Joystick

查看:74
本文介绍了Spritekit游戏杆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我改变操纵杆的方向时,我都试图对播放器精灵进行一些动画处理.

I'm trying to execute some animations of my player sprite whenever I change the direction of the joystick.

我正在使用 TheSneakyNarwhal在Joystick类中的放置方式,该方式使用以下方法:

I'm using TheSneakyNarwhal's drop in 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];
    }

但是,每当操纵杆的velocity.x大于0(向右移动)时,它都会从头开始调用该方法,实际上不会播放完整的动画.

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.

只有当我停止使用操纵杆时,它才能播放整个动画.

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.

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

Create a BOOL which you would set the first time the animation runs. Subsequent calls to the animation would check the BOOL's status and determine if the animation is already running. You can reset the BOOL once the joystick is no longer pointing right.

创建BOOL属性:

@property (nonatomic) BOOL animationRunning;

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

When your joystick values indicate you want your player to run right you call the animation method:

[self runRightAnimation];

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

Before the animation is run, the BOOL is checked to see if it is already running:

-(void)runRightAnimation {

    if(animationRunning == NO) {

        animationRunning = YES;

        // your animation code...
    }
}

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

Remember to set your animationRunning = NO; and stop the animation when the joystick is no longer in the desired position.

这篇关于Spritekit游戏杆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆