2014-02-26 27 views
11

我想在我的雪碧節點上運行兩個動畫,具體取決於它的旋轉。如果該值爲負值,則運行其中一個動畫,如果該值爲正則運行另一個動畫。我設法做到了(有點),但是我有一個問題。如果Animation1正在運行,並且zRotation更改爲正值,則它們都會運行,因爲它們會一直重複。所以我這樣做:停止SKYECTION RepeatsForever - 雪碧套件

NSMutableArray *walkingTextures = [NSMutableArray arrayWithCapacity:14]; 


for (int i = 1; i < 15; i++) { 
    NSString *textureName = 
    [NSString stringWithFormat:@"character%d", i]; 
    SKTexture *texture = 
    [SKTexture textureWithImageNamed:textureName]; 
    [walkingTextures addObject:texture]; 
} 

SKAction *spriteAnimation = [SKAction animateWithTextures:Textures timePerFrame:0.04]; 
    repeatWalkAnimation = [SKAction repeatActionForever:spriteAnimation]; 
    [sprite runAction:repeatWalkAnimation withKey:@"animation1"]; 

,然後當我希望它停止:

[self removeActionForKey:@"animation1"]; 

但它一直運行的操作,我怎麼能停止動作,然後呢?謝謝!

+2

變化[自removeActionForKey:@ 「動畫1」];到[sprite removeActionForKey:@「animation1」];你將不得不維護一個指向精靈的全局變量。 – ZeMoon

+0

@akashg您也可以命名精靈,然後在包含它的場景或節點中按名稱檢索它。 –

+0

是的,傑森是對的。這將更容易維護。 – ZeMoon

回答

17

該方法應該在運行SKAction的節點上調用。

變化

[self removeActionForKey:@"animation1"]; 

[sprite removeActionForKey:@"animation1"]; 
+0

還有一個問題,在這之前我有[sprite RunAction:Completion^{},現在我有[sprite RunAction:withKey:]我怎樣才能合併這兩個?用完成和withKey運行動作,這可能嗎?謝謝! – iSLB

+0

使SKAction成爲您想要的動作序列,並伴隨[SKAction runBlock:^ {}],然後使用[sprite RunAction:withKey:]傳遞此動作。 – ZeMoon

+0

糟糕,我忘記了使用repeatActionForever :.這會有點棘手。 – ZeMoon