Time for action – letting cannonballs collide with ships
To check if cannonballs collide against the enemy ship, follow these steps:
Open the
Ship.h
file.We need to add custom getters and setters to the
hitpoints
property, so let's make this propertynonatomic
and add an instance variable called_hitpoints
.Declare the methods
abortShooting
andhit
.Switch to the
Ship.m
file.The custom
hitpoints
getter just returns the instance variable_hitpoints
.The custom setter for
hitpoints
contains the following code:-(void) setHitpoints:(int)hitpoints { _hitpoints = hitpoints; if (_hitpoints <= 0) { self.visible = NO; } }
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; }
The
hit
method has the following content...