SpriteKit和SceneKit - 如何完全暂停游戏? [英] SpriteKit and SceneKit – How to completely pause a game?

查看:222
本文介绍了SpriteKit和SceneKit - 如何完全暂停游戏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功暂停了这段代码的场景游戏:

I succeeded to pause a scene game with this code:

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

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

But I have a problem. All random interval the game create objects and display them on the screen. When I pause the game it continues to create objects in background and when I resume the game all objects created during the pause appear in same time on the screen.

我该如何解决?

推荐答案

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

You can't add the SKLabelNode (or anything else) to your scene while the SKView is paused. You will need to return to the run loop so your text is added before pausing the game. Here's one way to do that:

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

这篇关于SpriteKit和SceneKit - 如何完全暂停游戏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆