2
我試過使用NSTimer,但不能讓我的頭繞它。 任何幫助,將不勝感激SpriteKit - 嘗試實現每秒增加一個2.d.p計時器。我怎樣才能做到這一點?
我試過使用NSTimer,但不能讓我的頭繞它。 任何幫助,將不勝感激SpriteKit - 嘗試實現每秒增加一個2.d.p計時器。我怎樣才能做到這一點?
你可以用SKActions做到這一點。例如...
SKAction *wait = [SKAction waitForDuration:1.0f];
SKAction *sequence = [SKAction sequence:@[[SKAction performSelector:@selector(methodToFire:) onTarget:self], wait]];
SKAction *repeat = [SKAction repeatActionForever:sequence];
[self runAction:repeat];
- (void)methodToFire {
...
}
如果你想要做什麼遊戲邏輯關係,這是很容易操縱的更新方法做你想做什麼。
-(void)update:(CFTimeInterval)currentTime
{
/* Called before each frame is rendered */
// Handle time delta.
// If we drop below 60fps, we still want things to happen at the same realtime rate.
CFTimeInterval timeSinceLast = currentTime - lastUpdateTimeInterval;
lastUpdateTimeInterval = currentTime;
[self updateWithTimeSinceLastUpdate:timeSinceLast];
}
- (void)updateWithTimeSinceLastUpdate:(CFTimeInterval)timeSinceLast
{
lastEventInterval += timeSinceLast;
//this is the part that will function like a timer selector and run once a second.
if (lastEventInterval > 1)
{
lastEventInterval = 0;
//do whatever you want to do once a second here
}
}
出於好奇,爲什麼CFTimeInterval而不是NSTimeInterval? – Roecrew
這是用於它們傳遞到更新方法的參數是什麼蘋果,所以它是我在其中使用。我也用NSTimeIntervals對於沒有問題類似的事情。我實際上不是在兩者之間的差異特別明顯 - 他們似乎都只是typedef定義雙打。 – webbcode