You are about to kill the boss and suddenly the door bell rings! Unfortunately, the game does not allow you to pause the game. You ran to the door but come back only to find out that the boss has killed you once again. We do not want that to happen when you are playing Space Demon hence we are going to implement a feature to pause and resume the game.

Why Pause?

Pausing allows you to freeze the game so it can be resumed at later time. Pausing the application also allows to bring user back to focus which was lost during an interruption. Not all applications need the pause feature. But this feature is considered mandatory for fast paced applications specially games.

Pausing and Resuming the Game:


There are multiple ways of pausing the game. In our game we will have a big red ugly button on the screen which when pressed creates a CCColorLayer on top of the current layer. The player can resume the game by pressed the "Resume" link on the PauseLayer.

Our first task is to add the pause button. This is performed in the Environment.m file as shown below:



Instead of using the CCSprite we have used the simplicity of CCMenuItemImage for our pause button. The screenshot below shows the pause button displayed in the game:



The above screenshot shows our big ugly pause button. When the pause button is pressed it triggers the "pauseGame" method. The "pauseGame" method is implemented below:



We use the isPaused method of the CCDirector class to find out if the game is already paused or not. If the game is not paused then we create a PauseLayer and add the layer on top of the current layer. After adding the PauseLayer to the scene we pause the current scene by calling the pause method on the CCDirector class.

Now, when the pause button is touched the PauseLayer appears on the screen as shown below:



The PauseLayer is a simple class that inherits from CCColorLayer as shown below:


 
Inside the initWithColor constructor we create a "Resume" label which will allow the user to resume the game. The resume label triggers the resumeGame method which is responsible for resuming the game. The resumeGame gets the current running scene and removes the PauseLayer using its unique tag and then resume the game by calling the resume method on the CCDirector.

This solves the basic scenario of pausing and resuming the game. But what happens when you are in the middle of killing the boss and someone calls you. We need to handle the scenario where you are interrupted by someone calling you. The good news is that Cocos2d framework has already given us few methods to work on this issues.

Open the delegate file which in our case is "HelloCocosAppDelegate.m" file and replace the applicationDidBecomeActive with the following implementation:



The above code will make sure that when you go back to the game it will be waiting for you to be resume.

Download the Complete Source Code:

The complete source code for the Space Demon game is available on Github. You can use the following URL to download the source.

https://github.com/azamsharp/Space-Demon

Conclusion:

In this article we learned how to pause the game during an interruption and then resume the game. In the future article we will demonstrate make use of the accelerometer input to control our spaceship.