Game Development Reference
In-Depth Information
This distance joint now allows absolutely no movement away from or toward its world
anchor. Even without the spring joint pulling on the spring, it will rest in place now. Give
it a try.
Caution
You may be tempted to see how far you can take this. Try increasing
the spring joint's stiffness from 90 to 900, and reduce damping to 0. Don't for-
get to reset these values after the test! You'll notice the spring board will not be
entirely at rest, and if you touch it slightly, it will already and significantly push
the player upward. This may give you ideas, and perhaps you can use it for a
good effect, but really the point here is that very strongly opposing joints (and
the forces they impose on the connected bodies) add unrest and inaccuracies to
the physics simulation. Even potentially harmless-looking objects can suddenly
infuse other objects with high velocities even though they aren't moving much
by themselves.
Releasing the Spring Lock
With the lock in place, the spring is no longer usable. You'll need to create a
SpringBoard
class to release the lock and then also put the lock back in place to reset
the springboard.
Select the
springboard_spring
node in
Spring.ccb
, and switch to the
Item Code Connec-
tions
tab. Enter
SpringBoard
as the custom class. Then switch to the
Item Physics
tab,
and enter
spring
in the
Collision type
field. This allows the
GameScene
class to imple-
ment a collision method that calls a
SpringBoard
method when the player touches the
springboard.
Now publish and open Xcode, but don't run the project just yet. First you have to create
another Objective-C class (by right-clicking the
Source
folder and selecting
New File
).
Name the class
SpringBoard
, and be sure to make it a subclass of
CCSprite
because the
springboard_spring
is a sprite.
Open the
SpringBoard.h
interface file. You only need to add a public method declaration
with which other objects can instruct the spring to let go of its lock. Add the code in
List-
ing 9-1
to
SpringBoard.h
.
Listing 9-1
.
The SpringBoard header declared the public letGo method
Search WWH ::
Custom Search