2016-02-28 60 views
1

我正在創建一個SpriteKit遊戲,該遊戲根據經過的時間量進行更新。遊戲使用NSTimer及其scheduledTimerWithTimeInterval方法產生敵人,每2.0秒調用一次spawnEnemy函數。爲什麼我的SpriteKit update()函數創建多個NSTimers?

當5秒鐘過去後,應該有一個非常短暫的間歇,以防止新的敵人產卵以顯示水平變化動畫。

當最初5秒鐘到達時,一切正常,直到有條件的地方self.nextLevelDelayTicker == 100。一旦滿足這個條件,"YOLO"字符串只在控制檯中被觸發一次。但是,我假設正在創建NSTimer的多個實例並將其存儲在self.timer之內,因爲在調用self.resumeGame()以創建新的計劃的計時器之後會產生大量的敵人。

爲什麼會發生這種情況的任何想法,即使我在我的條件下設置的標誌只能調用self.resumeGame()函數一次?

func resumeGame() { 
    // Start game timer 
    // Need a way to access ib action of pause button 
    self.timer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: "spawnEnemy", userInfo: nil, repeats: true) 
} 

override func update(currentTime: CFTimeInterval) { 
    /* Called before each frame is rendered */ 
    if gameTicker.isActive == true { 
     gameTicker.increment() 
    } 

    // If gameTicker is equal to 5 seconds, increase enemy amount 
    if gameTicker.getTimePassed() % 500 == 0 { 
     self.enemyAmount += 1 
     self.timer?.invalidate() 
     levelCount += 1 

     gameTicker.isActive = false 
    } 

    // If level has been completed and last ghost has been killed, activate next level scene 
    if gameTicker.isActive == false && enemyArray.count == 0 { 
     self.nextLevelDelayTicker.increment() 

     if self.nextLevelDelayTicker.getTimePassed() == 100 { 
      print("YOLO") 
      self.gameTicker.isActive = true 
      self.nextLevelDelayTicker.reset() 
      self.resumeGame() 
     } 
    } 
} 
+1

下面的答案值得考慮,但就你的問題而言,你創建了你的計時器變量並放置在更新中。每次更新被調用時,一個新的定時器被創建,因此多個定時器的想法雖然下面的答案是好的,但我喜歡使用skaction並在那裏創建我的定時器,並以適當的變量而不是更新方法調用它們。但那只是我。 –

回答

4

試圖遵循你的代碼..但我認爲你的方法對於spritekit並不是很好。這可能使事情變得比需要的複雜。

您可以使用您的更新方法直接跟蹤時間。這可能是值得重寫你的代碼的這部分。在spritekit中工作會更好,並且不易出錯。

所有你真正需要的是增量時間。

場景屬性

// time values 
var delta = NSTimeInterval(0) 
var last_update_time = NSTimeInterval(0) 

// some time youre trying to keep track of 
var timeLimit = NSTimeInterval(5) 
var timeLimitMax = NSTimeInterval(5) 

場景的更新方法

func update(currentTime: NSTimeInterval) { 
     if last_update_time == 0.0 { 
      delta = 0 
     } else { 
      delta = currentTime - last_update_time 
     } 

     last_update_date = currentTime 

     // now we can keep track of time 
     timeLimit -= self.delta 
     if timeLimit <= 0 { 
      // do something and reset timer 
      timeLimit = timeLimitMax 
     } 
} 

現在,如果你要始終如一地產卵東西秒每一個數字,然後我們甚至不需要費心用update來做到這一點。只需將其放入您的viewDidLoad

現在我們每兩秒鐘運行一次該代碼。最好的部分是這會暫停並自動恢復您的遊戲。你不必管理SKAction太多。 spritekit爲你做:)

let spawnAction = SKAction.repeatActionForever(
    SKAction.sequence([ 
     SKAction.waitForDuration(2), 
     SKAction.runBlock({ 
      [unowned self] in 
      self.spawnEnemy() 
     }) 
    ]) 
) 
runAction(spawnAction) 
+0

我提高了你的答案,因爲這是SpriteKit的方式,但你應該稍微改變一下spawnAction。首先,爲了編譯,你需要在塊內部明確的自我。其次,它會創建強大的參考週期,並且在轉換到下一個場景之後,場景永遠不會被釋放,因爲自己被捕獲並且動作是無止境的。因此,在轉換之前,無論是明確移除操作,還是使用塊內部的捕獲列表(無主自我)都可以解決問題。 – Whirlwind

+0

好點..我寫這個代碼沒有xcode所以我確定它不是100%完美。我會編輯。 – hamobi

+0

根據計時器產生敵人的相當好的答案。我擺脫了所有'NSTimer',並用一個SKAction替換了它們。暫停/恢復功能現在就像一個魅力。有一件事,我需要能夠每隔15秒暫停'spawnAction' SKAction 5秒鐘,以顯示動畫並防止敵人在此期間產卵。一旦5秒延遲結束,我需要能夠重新激活'SKAction'。有沒有可用的SKAction方法? – 10000RubyPools

相關問題