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

查看:66
本文介绍了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天全站免登陆