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

标签 swift 3d sprite-kit ios8 scenekit

我成功地用这段代码暂停了一个场景游戏:

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)
    }
}

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

我该如何解决?

最佳答案

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

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

关于swift - SpriteKit 和 SceneKit – 如何完全暂停游戏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25824984/

相关文章:

ios - 为什么 TableView 在某些行上添加了不可见的分隔符?

android - 为什么我的 OpenGL 球体看起来像一个椭圆

opengl - 使用 3D 纹理时结果不正确

ios - SpriteKit -- 更改 SKShapeNode 大小

objective-c - 如何比较 SKSpriteNode 纹理

ios - 加速 Sprite 套件的物理时钟

ios - 如何使搜索栏角变圆

c - Swift 3 中的 GLFW 回调签名

macos - 在 Swift 中逐行读取文件/URL

c++ - 3d 数组索引上的迭代器