2016-04-03 39 views
3

我想在2秒後用戶觸摸屏幕(它在touchesBegan裏面),但它不起作用addChild。我做錯了什麼?2秒後addChild

//show myLabel after 2 seconds 
self.myLabel.position = CGPoint(x: self.frame.width/1.1, y: self.frame.height/2) 
self.myLabel.text = "0" 
self.myLabel.zPosition = 4 

self.myLabel.runAction(SKAction.sequence([SKAction.waitForDuration(2.0), SKAction.runBlock({ 
    self.addChild(self.myLabel) 
)])) 

回答

4

的問題是,除非是在場景中的動作不會在myLabel運行,所以更改最後一部分爲:

self.runAction(SKAction.sequence([SKAction.waitForDuration(2.0), SKAction.runBlock({ 
    self.addChild(self.myLabel) 
})])) 

或者更好:

self.runAction(SKAction.waitForDuration(2)) { 
    self.addChild(self.myLabel) 
} 

注:我在這裏假設自己是一個場景或已經添加到場景中的其他節點。

+0

第二個工作,但我不得不改變一些事情(由Xcode的指導下使用)。它結束了: 'self.runAction(SKAction.waitForDuration(2.0),completion:{self.addChild(self.myLabel)})' – Luiz

+0

順便說一句,謝謝! =) – Luiz

+0

不客氣!我在Xcode中測試後編輯了我的答案。如果答案對您有幫助,請考慮投票和/或接受它:) – smallfinity

-1

你可以聲明此功能,您要

public func delay(delay:Double, closure:()->()) { 
    dispatch_after(
     dispatch_time(
      DISPATCH_TIME_NOW, 
      Int64(delay * Double(NSEC_PER_SEC)) 
     ), 
     dispatch_get_main_queue(), closure) 
} 

,並使用爲:

delay(2.0) { 
     self.addChild(self.myLabel) 
    } 
+0

不適合SpriteKit http://stackoverflow.com/a/23978854。 – Whirlwind

+0

@Whirlwind對不起,我忘了它。 –