O'Reilly logo

Learning Cocos2d-x Game Development by Siddharth Shekar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Keeping track of the score and the game over condition

To keep score, each time the player rocket hits the enemy, the player will get one point. If the enemy reaches the left of the screen or the enemy bullet hits the player, it should trigger the game over condition.

For this, create two global variables in the GameplayLayer.h file: an int and a bool variable. The int score variable will keep track of the score, and the bool gameOver variable will be used to check the game over condition:

    int score;
    bool gameOver;

In the constructor, initialize these two variables:

    score = 0;
    gameOver = false;

Next, as per our logic, the game should be over when any of the enemies goes beyond the left of the screen or any of the enemies' bullets hits the player. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required