SpriteKit和SceneKit - 如何完全暂停游戏?

Hao*_*aox 5 3d scenekit sprite-kit swift ios8

我成功地使用以下代码暂停场景游戏:

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

    var touch:UITouch = touches.anyObject() as UITouch 
    pauseText.text = "Continuer"
    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 {
        println("pause")
        addChild(pauseText)
        pause.removeFromParent()
        paused = true
    }
    if self.nodeAtPoint(locationPause) == self.pauseText {
        pauseText.removeFromParent()
        paused = false
        addChild(pause)
    }
}
Run Code Online (Sandbox Code Playgroud)

但我有一个问题.游戏的所有随机间隔创建对象并在屏幕上显示它们.当我暂停游戏时,它继续在后台创建对象,当我恢复游戏时,暂停期间创建的所有对象同时出现在屏幕上.

我该如何解决?

0x1*_*41E 9

在SKView暂停时,您无法将SKLabelNode(或其他任何内容)添加到场景中.您需要返回运行循环,以便在暂停游戏之前添加文本.这是一种方法:

// Add pause text or button to scene
addChild(pauseText)
let pauseAction = SKAction.run {
    self.view?.isPaused = true
}
self.run(pauseAction)
Run Code Online (Sandbox Code Playgroud)

  • 不立即执行将子节点添加到场景中.从touchesBegan返回后添加.scene.view.paused语句立即执行(或具有更高的优先级),因此在将SKLabelNode添加到场景之前执行. (2认同)