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 – adding more birds


We are going to add more Bird objects to fly around. To do this, we're going to replace our _bird variable with an array and write some code to spawn the birds:

  1. Open the GameScene.m file and remove the _bird variable. Instead, add two new instance variables to the GameScene class, as shown in the following code:

    @implementation GameScene
    {
        CCSpriteBatchNode *_batchNode;
        
        Hunter  *_hunter;
    
        float _timeUntilNextBird;
        NSMutableArray *_birds;
    }
  2. Add following method somewhere below the update: method:

    -(void)spawnBird
    {
        //1
        CGSize viewSize = [CCDirector sharedDirector].viewSize;
        
        //2
        int maxY = viewSize.height * 0.9f;
        int minY = viewSize.height * 0.6f;
        int birdY = minY + arc4random_uniform(maxY - minY);
        int birdX = viewSize.width * 1.3f;
        CGPoint birdStart = ccp(birdX, birdY);
        
        //3
        BirdType birdType = (BirdType)(arc4random_uniform(3));
        
        //4
        Bird* bird = [[Bird alloc] initWithBirdType...