O'Reilly logo

Learning iPhone Game Development with Cocos2D 3.0 by Kirill Muzykov

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

Time for action – detecting if the arrow hits the bird

To make the arrow hit the bird, we need to check that the arrow and the bird are intersecting. After we have found an intersection, we need to remove the bird and the arrow. Let's add the code to perform this:

  1. Open the Bird.h file and add the following method declaration:
    -(void)removeBird:(BOOL)hitByArrow;
  2. Then, open the Bird.m file and add its implementation:
    -(void)removeBird:(BOOL)hitByArrow
    {
        if (hitByArrow)
        {
            CCLOG(@"Bird hit by arrow");
        }
        else
        {
            CCLOG(@"Bird flew away");
        }
        
        [self removeFromParentAndCleanup:YES];
    }
  3. Open the GameScene.m file and add the _arrows array instance variable as follows:
    @implementation GameScene
    {
        //..skipped..
        NSMutableArray *_arrows;
    }
  4. Scroll to the init method and ...

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