I recently started developing my first iPhone app and it's going to be a 2D-game. Everything's going well so far and the project already includes more than a thousand lines of code.
However, I just stepped into a small problem. As with many games, there's a need to pause it. My first try was to simply use this in the code, where the pausing has to happen.
self.view.paused = true;
Worked fine until I needed some animations to happen after somebody hit the pause button. Imagine a new sprite node sliding up from the bottom of the screen that includes some detail about the current score and so on. Using the code above of course all animations would be stopped.
My new idea was to simply stop all nodes that had to be stopped individually. I got that working by using this chunk of code.
[self enumerateChildNodesWithName:@"name" usingBlock:^(SKNode* node, BOOL* stop) {
node.paused = true;
node.speed = 0.0;
}];
Tried a couple of runs and thought I had finally found a solution when I stepped into another problem. Some of the game's sprite nodes, which need to be stopped, are being created periodically after a certain amount of time. The following code is responsible for this behavior.
SKAction* createPeriodically = [SKAction repeatActionForever:[SKAction sequence:@[[SKAction performSelector:@selector(create) onTarget:self],[SKAction waitForDuration:15.0]]]];
[self runAction:createPeriodically];
As you can see I am using waitForDuration to have a delay between the creation of the sprite nodes. Now when I pause all nodes the time still goes on and thus the waitForDuration completes after the set amount of time and a sprite node is being created although all the others are paused and not moving.
I hope you guys were able to understand my problem. I'm looking for a way to pause the mechanism for creating the sprite nodes periodically while still being able to create a new sprite node afterwards and perform actions to the new one. Any help is appreciated. Thank you.
If you set paused
on a node, that pauses all actions for the node and its children. So you could pause all actions on the entire scene (including createPeriodically
) by setting the scene's paused
property, but then you'd be back in the same scenario as pausing the view.
But you can take advantage of that for the node and its children part to organize your scene. Instead of having all the nodes that make up your game be direct children of the scene, make a separate node to contain them all. Instead of running actions that affect your game content globally (like createPeriodically
) on the scene, run them on this separate node. Now, when you want to pause the game, set paused
on this node — all the game content actions will be paused, but there's still room to run actions on the scene itself or add other, non-paused nodes as direct children of your scene.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With