Game Development Reference
In-Depth Information
Behavior:
ExpireAfterTime
The class
ExpireAfterTime
is used by
Powerup
to remove it from the scene after a number of steps
have passed by. Before the power-up is removed, we want to make the power-up blink by changing
its state from glowing to not glowing. To support this second feature, we will want to have some sort
of communication between the
ExpireAfterTime
object and the actor it is acting on. Listing 6-25
shows the header of the class
ExpireAfterTime
.
Listing 6-25. ExpireAfterTime.h
#import <Foundation/Foundation.h>
#import "Actor.h"
@class ExpireAfterTime;
@protocol ExpireAfterTimeDelegate
-(void)stepsUpdated:(ExpireAfterTime*)expire In:(GameController*)controller;
@end
@interface ExpireAfterTime : NSObject <Behavior> {
}
@property (nonatomic) long stepsRemaining;
@property (nonatomic, assign) Actor<ExpireAfterTimeDelegate>* delegate;
+(id)expireAfter:(long)aNumberOfSteps;
@end
Here the class
ExpireAfterTime
conforms to the protocol Behavior, just like the class
LinearMotion
does.
ExpireAfterTime
also defines a new protocol called
ExpireAfterTimeDelegate
that defines
the task
stepsUpdated:In
:, which will be called by the
ExpireAfterTime
on its delegate every time
applyToActor:In
: is called, as shown in Listing 6-26.
Listing 6-26. ExpireAfterTime.m
#import "ExpireAfterTime.h"
#import "GameController.h"
@implementation ExpireAfterTime
@synthesize stepsRemaining;
@synthesize delegate;
+(id)expireAfter:(long)aNumberOfSteps {
ExpireAfterTime* expires = [ExpireAfterTime new];
[expires setStepsRemaining: aNumberOfSteps];
return expires;
}
-(void)applyToActor:(Actor*)anActor In:(GameController*)gameController{
stepsRemaining--;
[delegate stepsUpdated:self In:gameController];