2014-04-04 69 views
2

的速度,我有這樣的代碼:修改正在運行的SKAction

@implementation MyScene { 
SKAction *delayAction; 
} 
Inside a method: 

delayAction = [SKAction waitForDuration:3.0]; 
[self runAction:[SKAction repeatActionForever: [SKAction sequence: 
               @[delayAction, [SKAction ...]]]]] 
                withKey:@"myKey"]; 

然後我希望縮短加班。 (這種方法被稱爲上更新:) 所以我嘗試:

- (void)updateVelocity 
{ 
    NSLog(@"duration:%f",delayAction.duration); 
    delayAction.duration = delayAction.duration - 0.001; 
} 

,我也得到:

2014-04-04 11:45:05.781 FlyFish[5409:60b] duration:1.300000 
2014-04-04 11:45:05.785 FlyFish[5409:60b] duration:1.299000 
2014-04-04 11:45:05.800 FlyFish[5409:60b] duration:1.298000 
2014-04-04 11:45:05.816 FlyFish[5409:60b] duration:1.297000 

這似乎不錯,但我的[SKAction ...]依然延續了在3秒後重復。

回答

8

我會以不同的方式做到這一點。事情是這樣的......

- (void)recursiveActionMethod 
{ 
    if (some end condition is met) { 
     return; 
     // this allows you to stop the repeating action. 
    } 

    self.duration -= 0.01; 
    // store duration in a property 

    SKAction *waitAction = [SKAction waitForDuration:self.duration]; 
    SKAction *theAction = [SKAction doWhatYouWantHere]; 
    SKAction *recursiveAcion = [SKAction performSelector:@selector(recursiveActionMethod) onTarget:self]; 

    SKAction *sequence = [SKAction sequence:@[waitAction, theAction, recursiveAction]]; 
    [self runAction:sequence]; 
} 

這將對您的操作,然後回來這個功能被再次使用不同的等待時間又再次運行和,和...

你甚至可以通過讓一些結束條件在if塊內跳轉並停止循環來停止序列。

+1

很好的解決方案。無論如何,我解決與不同的aproax。我用actionForKey搜索了該動作:然後將其刪除。之後,我用diffenret waitForDuration再次啓動它。 – Godfather

2

我有點晚,但您可以改爲將操作設置爲SKActionTimingEaseOut。這也是本地語言,應該稍快一點。 (雖然你不能自定義的速度變化) 這可以同樣做到這樣:

  1. yourSKAction.timingMode = SKActionTimingEaseOut;