2014-09-13 82 views
2

我試圖通過觸摸按鈕暫停我的遊戲,但無法使其工作。如何在SpriteKit和SceneKit中暫停遊戲 - SWIFT

我嘗試這樣做:

pause = SKSpriteNode (imageNamed: "pause.png") 
pause.size = CGSizeMake(30, 30) 
pause.position = CGPointMake(30, 30) 
self.addChild(pause) 

,我試圖調用這樣的功能:

func touchesBegin (touches: NSSet!, withEvent event: UIEvent!) 
{ 

    for touch: AnyObject in touches 
    { 
     let location = touch.locationInNode(self) 
     if self.nodeAtPoint(location) == self.pause 
     { 
      let skView = self.view as SKView 
      skView.paused = true 
     } 
    } 

} 

當我觸摸暫停按鈕沒有任何反應...... 出了什麼問題?

謝謝!

回答

3

我自己解決了這個問題:

這裏我的代碼功能:

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) 
    { 
    var touch:UITouch = touches.anyObject() as UITouch 
    pauseText.text = "Pause" 
    pauseText.fontSize = 50 
    pauseText.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2) 

        /* bouton play/pause */ 

    var locationPause: CGPoint = touch.locationInNode(self) 
    if self.nodeAtPoint(locationPause) == self.pause 
     { 
     addChild(pauseText) // add the text 
     pause.removeFromParent() // to avoid error when you touch again 
     self.runAction (SKAction.runBlock(self.pauseGame)) 
     } 

    } 

要恢復你只要之前的最後一個「}」添加此代碼的遊戲:

if self.nodeAtPoint(locationPause) == self.pauseText 
     { 
     pauseText.removeFromParent() // remove the pause text 
     self.view.paused = false // resume the game 
     addChild(pause) // add the pause button 
     } 

而在SKScene子類中添加此功能可在暫停期間添加標籤:

func pauseGame() 
    { 
    self.view.paused = true // to pause the game 
    }