Game Development Reference
In-Depth Information
In Listing 8-11, we see this definition of the class TemporaryBehavior . We see that it defines a
protocol called TemporaryBehaviorDelegate , which TapGestureController conforms to (see
Listing 8-8). The constructor for TemporaryBehavior takes a behavior and a number of steps as
arguments. Listing 8-12 shows the implementation of TemporaryNehavior .
Listing 8-12. TemporaryBehavior.m
+(id)temporaryBehavior:(NSObject<Behavior>*)aBehavior for:(long)aNumberOfSteps{
TemporaryBehavior* temp=[TemporaryBehavior new];
[temp setBehavior:aBehavior];
[temp setStepsRemaining:aNumberOfSteps];
return temp;
}
-(void)applyToActor:(Actor*)anActor In:(GameController*)gameController{
stepsRemaining--;
[behavior applyToActor:anActor In:gameController];
[delegate stepsUpdatedOn:anActor By:self In:gameController];
if (stepsRemaining<= 0){
[[anActor behaviors] removeObject:self];
}
}
@end
In Listing 8-12, we see the implementation of the class TemporaryBehavior . The constructor simply
creates a TemporaryBehavior object and populates it with the provided arguments. The task,
applyToActor:In :, comes from the protocol Behavior (which TemporaryBehavior conforms to), and
is called for every step of the game. In this task, we simply apply the provided behavior to the actor
and inform the delegate that this work has been done. Continuing with the tap gesture example,
let's look at how TapGestureController responds to the task stepsUpdatedOn:By:In :, as shown in
Listing 8-13.
Listing 8-13. TapGestureController.m (stepsUpdatedOn:By:In:)
-(void)stepsUpdatedOn:(Actor*)anActor By:(TemporaryBehavior*)tempBehavior In:(GameController*)
controller{
if ([tempBehavior stepsRemaining]==0){
[anActor setState:STATE_NO_GLOW];
}
}
In Listing 8-13, we see the task stepsUpdatedOn:By:In : as defined by the class
TapGestureController . In this task, we simply see if the TemporaryBehavior, tempBehavior , is at the
end of its life. If it is, we set the actor's state to STATE_NO_GLOW, which stops the power-up from
spinning.
Search WWH ::




Custom Search