Book Image

Sparrow iOS Game Framework Beginner's Guide

By : Johannes Stein
Book Image

Sparrow iOS Game Framework Beginner's Guide

By: Johannes Stein

Overview of this book

Table of Contents (20 chapters)
Sparrow iOS Game Framework Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Afterword
Index

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];
        [Sparrow.juggler removeObjectsWithTarget:self.cannonBallRight];
        
        self.cannonBallLeft.visible = NO;
        self.cannonBallRight.visible = NO;
    }
  8. The hit method has the following content...