Game Development Reference
In-Depth Information
File nodes “inherit” the nodes of a CCB file, even though these nodes aren't
shown in the Timeline.
You'll now need to assign the
CCPhysicsNode
reference to the
_physicsNode
ivar
in order to make the scrolling code work with the newly introduced physics node. Because
getChildByName:
returns a reference to a
CCNode
class, you have to cast the re-
turned node if it is of another class. Add this code at the top of the
loadLevelNamed:
method in
GameScene.m
:
_physicsNode = (CCPhysicsNode*)[self
getChildByName:@"physics" recursively:YES];
Note that casting an object reference to another class like this never generates a compile
error. That doesn't mean it's always correct, however. At runtime, if the particular object
is cast to a class of which the object isn't a member of, sending messages to this object is
prone to crashing with “unrecognized selector sent to instance” or other errors. Only use
casts when you are absolutely sure that an object is of a given class. If you can't be sure of
this at compile time, it is customary to verify the class membership with the
isKindOfClass:
method before performing the cast, as in the following code:
CCNode* node = [self getChildByName:@"physics"
recursively:YES];
if ([node isKindeOfClass:[CCPhysicsNode class]])
{
_physicsNode = (CCPhysicsNode*)node;
}
While you're at it, add the following line to assign the
_backgroundNode
reference,
also in the
loadLevelNamed:
method:
backgroundNode = [self getChildByName:@"background"
recursively:YES];
excluding any comments and error-checking code.
Listing 3-9
.
Assigning physics, background, and player node in loadLevelNamed
Search WWH ::
Custom Search