Game Development Reference
In-Depth Information
prite class in the Box2D collision callback methods. Because you're using ARC,
you need to __bridge cast self to void* to tell the compiler that self is passed
into the C++ method SetUserData as a weak reference. In other words, you're
telling ARC that Box2D won't release the memory of the self object, and that you
accept that if self is released from memory by ARC, the body's userdata will be
an invalid pointer. Because BodySprite releases the Box2D body when it's released,
this won't cause any issues because the body is released from memory before the
self object.
Tip The CCNode class also has a userData property, which you can use in
the same way as b2Body 's userData field, including the requirement to use
(__bridge void*) casts. But CCNode also has a userObject property,
which is of type id and has the same purpose as userData . If you need to
store an arbitrary Objective-C class object in a CCNode object, use the user-
Object property to avoid having to perform a __bridge cast.
The PhysicsEditor GB2ShapeCache class is used to add the fixtures to the body us-
ing the provided shapeName . The PhysicsBox2DPinball01 project already in-
cludes the necessary files. To make use of the GB2ShapeCache class in a new pro-
jects, just remember to add the GB2ShapeCache.h and GB2ShapeCache.mm files
from the PhysicEditor.dmg disk image folder /Loaders/generic-box2d-
plist to your Xcode project and then re-run the Convert to Objective-C ARC process
described earlier.
The key point to take away here is that the BodySprite is a CCSprite that man-
ages the allocation, deallocation, and configuration of the Box2D body for you. It also
ensures that you can access the BodySprite class and thus the cocos2d sprite
wherever you normally have access only to the Box2D body object. Thus, the BodyS-
prite class becomes the glue that brings the physics body together with the cocos2d
sprite.
If a sprite derived from BodySprite goes out of scope—for example, if you remove
it as a child from its cocos2d parent node—then BodySprite will take care of des-
troying the Box2D body for you. This is the most important distinction from cocos2d's
PhysicSprite class, which only gets you halfway there.
Search Nedrilad ::




Custom Search