2015-08-21 61 views
0

我正在嘗試unhide此節點,以便它出現在rect位置內,但隨後又以某種方式再次使該節點hidden處於脫機狀態,因此當它位於rect內的不同位置時。但我試圖隨機添加一個隨機定時器unhides(但不到5秒)?節點的新位置

let x12 = rect4.origin.x + CGFloat(arc4random()) % rect4.size.width 
let y12 = rect4.origin.y + CGFloat(arc4random()) % rect4.size.height 
    let randomPoint12 = CGPointMake(x12, y12) 
    self.yellowcircle3.position = randomPoint12 
    self.addChild(yellowcircle3) 


    yellowcircle3.hidden = true 

回答

0

這聽起來像你想要一個NSTimer並使用arc4random得到一個時間間隔。

你需要做的第一件事是通過添加代碼聲明的NSTimer和隨機數:

var myTimer = NSTimer() 
var secondsPassed : Int = 0 
var randomTimeInterval = arc4Random % 4 + 1 

(4 + 1將返回1之間的值和5)

下一頁,當你想移動你的節點時,你需要一個函數來調用。

func moveNode(){ 
      myTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timerRan:"), userInfo: nil, repeats: true) 

    } 

一旦您撥打moveNode(),您的計時器將開始運行。但現在我們需要定時器的功能,或者它是Selector,所以它可以正常運行。

func timerRan(sender: NSTimer!){ 
    secondsPassed++ 
    if secondsPassed == randomTimeInterval (
     //unhide your rect and reset the timer 
    ) 
} 

希望這有助於:)

+0

我建議你使用,而不是'NSTimer'的'SKAction'。這是爲什麼http://stackoverflow.com/questions/23978209/spritekit-creating-a-timer – 0x141E

+0

我可以做到這一點與多個節點? – MattB

+0

@MattB格式與多個節點相同。您只需要爲每個節點分別定義多個定時器,例如myTimer1和myTimer2 –