How to completely pause the game? SpriteKit / SceneKit [SWIFT]

I managed to pause the scenario game using 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 game intervals create objects and display them on the screen. When I pause the game, it continues to create objects in the background, and when I resume the game, all objects created during the pause appear simultaneously on the screen.

How can i fix this?

+5
source share
1 answer

You cannot add SKLabelNode (or anything else) to your scene while SKView is paused. You will need to return to the execution loop so that your text is added before the game is paused. Here is one way to do this:

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

Source: https://habr.com/ru/post/1202502/


All Articles