I'm trying to make a small game using SpriteKit. ( Xcode 8 and Swift 3 for iOS 10 )
I want to let the user pause the game when he touches the Pause text label. When he does that, I want to pause the game and change the label text to Unpause.
Below is my code for that part.
if ( pauseTextLabel.contains(touchLocation!)){
gameLoopPaused = !gameLoopPaused;
if ( pauseTextLabel.text == "Pause" ){
// pauseTextLabel.text = "Unpause";
pauseTextLabel = SKLabelNode(fontNamed: gameData.fontName);
initLabel(label: pauseTextLabel, gameData: gameData, text: "Unpause", pos: CGPoint(x: 3 * self.size.width/4 , y: self.size.height - 50 ) );
self.addChild(pauseTextLabel);
runPauseAction();
} else {
pauseTextLabel.text = "Pause";
runUnpauseAction();
}
}
I tried doing this first, It didn't work.
if ( pauseTextLabel.contains(touchLocation!)){
gameLoopPaused = !gameLoopPaused;
if ( pauseTextLabel.text == "Pause" ){
pauseTextLabel.text = "Unpause";
runPauseAction();
} else {
pauseTextLabel.text = "Pause";
runUnpauseAction();
}
}
I added a break point just before that block and this block shows no messages in the console. The game is being paused and I am able to resume it. However the only thing not working is the display text updating. I tried to remove it and add it to the view. It didn't work.
Any help or hints what went wrong is really appreciated.
Edit: I modified the touchesBegan as suggested. I am including my runPauseAction() to provide more information in case that matters.
private func runPauseAction(){
print(#function);
//self.pauseTextLabel.text = "Resume";
self.view?.isPaused = true;
self.physicsWorld.speed = 0.0;
}