Book Image

Learning iPhone Game Development with Cocos2D 3.0

By : Kirill Muzykov
Book Image

Learning iPhone Game Development with Cocos2D 3.0

By: Kirill Muzykov

Overview of this book

Table of Contents (19 chapters)
Learning iPhone Game Development with Cocos2D 3.0
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – winning or losing the game


In most games, we can win or lose. This makes the game interesting. In our game, we will win the game if we avoid losing. To be more specific, we will lose if we allow some number of the birds to fly away, for example, 3 birds out of 20. If we'll hit more than 17 birds, then we'll win.

Let's add the code to win and lose the game:

  1. Open the GameScene.h file and add enum with game states and a property to hold the current state, as shown in the following code:

    #import "CCScene.h"
    
    typedef enum GameState
    {
        GameStateUninitialized,
        GameStatePlaying,
        GameStatePaused,
        GameStateWon,
        GameStateLost
        
    } GameState;
    
    @interface GameScene : CCScene
    
    @property (nonatomic, assign) GameState gameState;
    
    @end
  2. Switch to the GameScene.m file and add the following instance variables:

    @implementation GameScene
    {
        //..skipped..
        int _birdsToSpawn;
        int _birdsToLose;
    }
  3. Initialize the state property and the variables that we've added before in...