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:
- Open the
Bird.h
file and add the following method declaration:-(void)removeBird:(BOOL)hitByArrow;
- 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]; }
- Open the
GameScene.m
file and add the_arrows
array instance variable as follows:@implementation GameScene { //..skipped.. NSMutableArray *_arrows; }
- Scroll to the
init
method and ...
Get Learning iPhone Game Development with Cocos2D 3.0 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.