Game Development Reference
In-Depth Information
Unfortunately, the Cocos2d engine exposes only the generic
CCPhysicsJoint
class
publicly. All the other concrete joint classes, like
CCPhysicsSpringJoint
and
CCPhysicsSlideJoint
(the distance joint), are private. To avoid importing a private
header or performing other trickery, the
Class
type of the
CCPhysicsSlideJoint
is
obtained by its name. Of course, a class by that name still has to exist; otherwise,
NSClassFromString
would return nil.
In
didLoadFromCCB
, the list of
self.physicsBody.joints
is enumerated.
Since the springboard contains only two joints, and they are of a different type, the search
can be simplified to a simple
isKindOfClass
check. Once the first distance joint is
found, it is assigned to the
_lockJoint
ivar and the loop ends with the
break
keyword.
Tip
The “find my joint” code in
Listing 9-2
may cease to function when you
add more distance joints to the
Spring.ccb
. Unfortunately, there's no name or
tag for joints that helps you to identify them easily, neither in SpriteBuilder nor
in code. However, you can give the joint's nodes a unique name to identify
them. You can then access the joint's bodies and their nodes in code and com-
pare the node names to find out if they are the nodes you are looking for. For
if ([joint.bodyA.node.name isEqualToString:@"node
A"] &&
[joint.bodyB.node.name isEqualToString:@"node
B"]) {
// do something
}
With the distance joint assigned to
_lockJoint
, implementing the
letGo
method is
Listing 9-3
.
Invalidating the _lockJoint will remove it from the world
-(void) letGo
{
if (_lockJoint.valid)
{
[_lockJoint invalidate];
Search WWH ::
Custom Search