Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to effectively pause a game in spritekit?

This seems like such a basic concept but there is really no good answer.

I currently have a pretty basic setup, in my game's scene, I have an enum gameState which is currently either inGame or gamePaused.

I have my button set up, and I have it so pressing simply toggles gameState.

Then, to make it work, I have two separate update methods: One for when gamePaused, one for when inGame.

This works for 95% of my game, as the updates in my game can be pretty readily started and stopped without any issues. There are some issues I cannot tackle though.

First and foremost are actions. I use SKActions for a bit of my project (some movement, scaling, fading...) and this method does NOT pause them. This can be catastrophic in certain points. Secondly, this doesn't handle particles, physics, and a few other things that are not directly associated to my update methods.

The only clue I have is self.view.paused = YES, but that isn't going to work either.

First off, this DOES fix my actions, particles, and physics problem... kinda. But This question suggests that SKActions aren't actually PAUSED by this, but actually STOPPED completely. Is this true? If it is, then it won't work smoothly either because my actions are thrown out of whack.

Also, pausing the view seems to do what it says: stop everything. EVERYTHING. Newbie question here, but how am I supposed to get any code to run at that point? It's all cut off. Is this where I need subViews? I have never used a subView yet, but it sounds like its what I want in this case.

Simply put, there are a lot of unanswered questions for me, and I don't know how to proceed. I'm hoping there's some 'standard procedure' for pausing in Sprite Kit, but with pausing the view halting actions I truly have no idea where to proceed.

Should I move away from using actions? Is my idea of a pause subView sound?

I don't want to babble, all I want to know is how you go about pausing in your average Sprite Kit project. Additional info provided upon request.

like image 289
IAmTheAg Avatar asked May 07 '14 03:05

IAmTheAg


3 Answers

Pausing a node will pause (not stop) the node's actions, and i suppose pause will also be applied to all children of the paused node in the same way. All other unpaused nodes will continue to run their actions to completion.

You should have a "game layer" node with all game nodes in them that you want to pause, and then just pause that "game layer" node. Then have a "pause game menu" node where you add all the pause menu stuff you need which will continue to function normally even if the game layer is paused.

Be very sure you do not use performSelector or NSTimer or GCD or any other scheduling methods besides actions or the scene's update: method because those will not adhere to a node's paused state.

Pausing the view effectively freezes everything in it, pausing the scene according to some will not pause the update: calls - but I have not verified this myself. If this were the case you can always check inside the update method whether a given node that you send a message to is paused and if so, decide not to send certain messages to that node.

like image 196
LearnCocos2D Avatar answered Oct 21 '22 15:10

LearnCocos2D


I just called a map function on all children of the scene and set the paused property to true

self.children.map{($0 as SKNode).paused = false}

By setting it to true you can easily undo the freeze.

EDIT: Better use a for-loop to iterate over the children.

for node in self.children as [SKNode] {
    node.paused = false
}
like image 21
Nick Podratz Avatar answered Oct 21 '22 16:10

Nick Podratz


I can tell you what I did in my game:

  1. Pause the SKView on -applicationWillResignActive: (or equivalent, using NSNotifications),

  2. Un-pause the SKView on -applicationDidBecomeActive: (or equivalent, using NSNotifications),

For the actual game scene only, I set a boolean flag _isPaused when resigning active, and if it is true I just skip the -update: method (frame updates). (I also show the "paused" menu when the user resumes the app).

...But my game is a simple puzzle game, and there's no long actions that should continue past the pause/resume. I can not confirm right now that all actions are aborted, but I think it's not the case.

like image 29
Nicolas Miari Avatar answered Oct 21 '22 17:10

Nicolas Miari