簡體   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