Game Development Reference
In-Depth Information
Listing 9-6
.
Importing SpringBoard.h in GameScene.m
#import "GameScene.h"
#import "Trigger.h"
#import "GameMenuLayer.h"
#import "GameState.h"
#import "SpringBoard.h"
Then add the method in
Listing 9-7
just below the other
ccPhysicsCollisionBe-
gin
methods.
Listing 9-7
.
Responding to collisions between player and spring
-(BOOL)
ccPhysicsCollisionBegin:(CCPhysicsCollisionPair*)pair
player:(CCNode*)player
spring:(SpringBoard*)spring
{
[spring letGo];
return YES;
}
Now when the player collides with a physics body whose
Collision
type is set to
letGo
method. Here we can safely assume that all springs are instances of the
SpringBoard
class; therefore, it's safe to assume the
spring
parameter to be of type
SpringBoard*
rather than
CCNode*
.
Returning YES from the method ensures the player and spring do collide rather than
passing through each other.
That's it for the spring. You can now place instances of
Spring.ccb
in the level. You
should add them to the
chains & ropes & springs
node. When you run the game and touch
a spring with the player, the spring should accelerate outward, pushing the player away.
You can alter the spring joint's
damping
and
stiffness
parameters as well as the po-
sition of the spring's world hook (by editing
Anchor A
) to alter the spring force.
Breaking Joints and Motors
Search WWH ::
Custom Search