Game Development Reference
In-Depth Information
Powerup* powerup=[powerups objectAtIndex:index];
TemporaryBehavior* tempBehav=[TemporaryBehavior temporaryBehavior:nil for:60*5];
[tempBehav setDelegate:self];
NSMutableArray* behaviors=[powerup behaviors];
[behaviors removeAllObjects];
[behaviors addObject:tempBehav];
[powerup setState:STATE_GLOW];
}
tapGesture
:, which is called whenever one of the 12
recognizes a tap gesture. Note that we are not checking the state of
UITapGestureRecognizer
; this is because tap gestures are considered discrete and don't use
numberOfTapsRequired
and
numberOfTouches
to figure
UITapGestureRecognizer
has responded and store these values in taps and touches,
Powerup
that should be enabled and is found in
NSMutableArray powerups
. Once we have the correct
Powerup
, we create a
TemporaryBehavior
self
as the delegate. The power-up then has its behavior set to the newly created
, as well as having its state set to STATE_GLOW. This starts the power-up
TemporaryBehavior
.
The
TemporaryBehavior
Class
The class
TemporaryBehavior
applies a behavior to an actor for a fixed number of steps. In this case,
we don't really want to apply any behaviors; we just want to know when five seconds has gone by.
Let's take a quick look the class
TemporaryBehavior
and see how this works. The declaration of
TemporaryBehavior
is shown in Listing 8-11.
Listing 8-11. TemporaryBehavior.h
@class TemporaryBehavior;
@protocol TemporaryBehaviorDelegate
-(void)stepsUpdatedOn:(Actor*)anActor By:(TemporaryBehavior*)tempBehavior In:(GameController*)
controller;
@end
@interface TemporaryBehavior : NSObject<Behavior>{
}
@property (nonatomic) long stepsRemaining;
@property (nonatomic, strong) NSObject<Behavior>* behavior;
@property (nonatomic, strong) NSObject<TemporaryBehaviorDelegate>* delegate;
+(id)temporaryBehavior:(NSObject<Behavior>*)aBehavior for:(long)aNumberOfSteps;
@end