Book Image

Learning Cocos2d-x Game Development

By : Siddharth Shekar
Book Image

Learning Cocos2d-x Game Development

By: Siddharth Shekar

Overview of this book

Table of Contents (19 chapters)
Learning Cocos2d-x Game Development
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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...