Game Development Reference
In-Depth Information
Responding to Contact Events
Now whenever the ball and the plunger get in contact, the be-
ginContactWithBall method in the Plunger class is called:
-(void) endPlunge:(ccTime)delta
// stop the motor
joint- > EnableMotor(NO);
-(void) beginContactWithBall:(Contact*)contact
// start the motor
joint- > EnableMotor(YES);
// schedule motor to come back, unschedule in case the plunger is hit repeatedly
[self scheduleOnce:@selector(endPlunge:) delay:0.5f];
As soon as the ball touches the plunger, the plunger's motor is enabled, which propels
the plunger and thus the ball upward. The endPlunge method is scheduled to stop
the motor after a short time. I took extra care to correctly unschedule the selectors. For
example, it's very likely that the beginContactWithBall method is called re-
peatedly within a short time period, because there may be more than one contact point
(Box2D reports each contact point individually); or simply, the ball might bounce a
little and lose contact, but the plunger's motor ensures that the plunger will touch the
ball again after a short time.
Similarly, you'll find the two methods beginContactWithPlunger and be-
ginContactWithBumper implemented in the Ball class. Both types of contact
will simply play a sound effect:
-(void) playSound
float pitch = 0.9f + CCRANDOM_0_1() * 0.2f;
float gain = 1.0f + CCRANDOM_0_1() * 0.3f;
[[SimpleAudioEngine sharedEngine] playEffect:@"bumper.wav"
Search Nedrilad ::

Custom Search