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

Get Learning Cocos2d-x Game Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.