How to prevent SKAction from starting on paused scene (after failure), node texture does not change after scene pause / pause
I have two problems pausing and stopping a scene. I have a button:
playPause = SKSpriteNode(imageNamed: "buttPause.png")
playPause.name = "pause"
playPause.setScale (0.65)
playPause.position = CGPoint(x: -self.frame.width / 2.55 , y: self.frame.height / 2.27)
playPause.zPosition = 10
self.addChild(playPause)
I have set a function to pause and unlock the scene + change the texture of the button:
func buttonpauseplayTouched() {
if isPlaying {
playPause.texture = SKTexture(imageNamed: "buttPlay")
isPlaying = false
self.scene?.view?.isPaused = true
}
else {
playPause.texture = SKTexture(imageNamed: "buttPause")
isPlaying = true
self.scene?.view?.isPaused = false
}
}
I pressed the button:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
location = touch.location(in: self)
node = self.atPoint(location)
if node.name == "pause" {
buttonpauseplayTouched()
}
else {print ("Blank space")}
}
}
Now when I touch the pause button, I can pause and unlock the scene, but the texture doesn't change? What's wrong? Or if I want to add another spritekitnode to the scene while it is paused, I cannot.
The second problem is that I have other SKSpriteNodes in the scene and I set the action when I touch them. If I touch them while the scene is stopped, nothing happens, but when I pause the scene, the action on the object is triggered. How can I prevent when the scene is paused that I cannot perform an action on the object. I'm trying to:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
location = touch.location(in: self)
node = self.atPoint(location)
if node.name == "pause" {
buttonpauseplayTouched()
}
else if scene?.view?.isPaused == true && node.name == "object" {print ("Nothing!!")}
}
else {print ("Blank space")}
}
}
No success. Thanks for any feedback.
UPDATE:
Thanks to both of you! The layered solution is better, I try and it works great! One small problem has to do with moving the background:
override func didMove(to view: SKView) {
createBackground()
}
func createBackground(){
for i in 0...3 {
background = SKSpriteNode(imageNamed: "background1.png")
background.name = "background"
background.setScale(0.694)
background.position = CGPoint(x: 0, y: CGFloat(i) * background.size.height)
background.zPosition = 1
gameLayer.addChild(background)
}
}
func moveBackground(){
gameLayer.enumerateChildNodes(withName: "background", using: ({
(node, error) in
node.position.y -= 7
if node.position.y < -((self.background.size.height)) {
node.position.y += (self.background.size.height) * 3
}
}))
}
override func update(_ currentTime: TimeInterval) {
moveBackground()
}
When I pause the gameLayer, the background is still moving. How do I edit the code to stop the bacground from moving when game mode is paused? I hope that only a small change in the code will solve this. Thank you!
source to share
I have placed my controls on my own layer (SKNode), controllingLayer.addChild (pauseButton) and game objects on my own “gameLayer” layer, and then when I want to pause the game, I just pause the gameLayer (gameLayer.isPaused = true) ... This stops the movement of game objects, creates a feeling of pause, but still allows me to do things like actions, add objects, etc.
override func update(_ currentTime: TimeInterval) {
if !gameLayer.isPaused {
moveBackground()
}
}
source to share
I'm personally a fan of what Ron said, but just want to show you how you can do it in a few lines:
func buttonpauseplayTouched() {
playPause.texture = SKTexture(imageNamed: isPaused ? "buttonPlay" : "buttonPause")
//pausing the scene rather than a view
self.isPaused = !self.isPaused
}
So basically based on the property isPaused
you choose the texture. And after that you will pause the scene. In this case, you don't need a variable isPlaying
.
source to share