O'Reilly logo

Sparrow iOS Game Framework Beginner's Guide by Johannes Stein

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 – letting cannonballs collide with ships

To check if cannonballs collide against the enemy ship, follow these steps:

  1. Open the Ship.h file.
  2. We need to add custom getters and setters to the hitpoints property, so let's make this property nonatomic and add an instance variable called _hitpoints.
  3. Declare the methods abortShooting and hit.
  4. Switch to the Ship.m file.
  5. The custom hitpoints getter just returns the instance variable _hitpoints.
  6. The custom setter for hitpoints contains the following code:
    -(void) setHitpoints:(int)hitpoints
    {
        _hitpoints = hitpoints;
        if (_hitpoints <= 0) {
            self.visible = NO;
        }
    }
  7. The abortShooting method consists of the following lines:
    -(void) abortShooting { _isShooting = NO; [Sparrow.juggler removeObjectsWithTarget:self.cannonBallLeft]; ...

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