Implementing pause and resuming the game
Now, we implement the pause and resume buttons in HelloWorldScene
:
We will add the gamePaused()
and gameResumed()
functions in the HelloWorldScene
class. For this, create two functions in the HelloWorldScene.h
:
void gamePaused(); void gameResumed();
Next, we define these functions in the HelloWorldScene.cpp
file:
void HelloWorld::gamePaused() { this->unscheduleUpdate(); this->unschedule(schedule_selector(HelloWorld::spawnEnemy)); if(gameplayLayer->getEnemiesArray()->count() >0) { for(int i=0; i< gameplayLayer->getEnemiesArray()->count(); i++) { Enemy* en = (Enemy*) gameplayLayer->getEnemiesArray()->objectAtIndex(i); en->pauseSchedulerAndActions(); } } }
Once the game is paused, we unschedule the update and enemy pause functions, cycle through all the enemies, and unschedule all the functions on the layer. This is similar to what we did in the gameOver...