Game Development Reference
In-Depth Information
-(void) loadLevelNamed:(NSString*)levelCCB
{
_physicsNode = (CCPhysicsNode*)[self
getChildByName:@"physics" recursively:YES];
_backgroundNode = [self getChildByName:@"background"
recursively:YES];
_playerNode = [self getChildByName:@"player"
recursively:YES];
}
You can optionally add statements like NSAssert(_physicsNode); to verify that
these nodes were indeed found and assigned. If they aren't found and nil is returned, you
either forgot to add them to the level or you named them incorrectly. Both issues are a
common source of (human) error when working with SpriteBuilder, so it makes sense to
verify references using NSAssert .
Tip If you want to learn more about assertions, breakpoints, and resolving is-
sues in general, please refer to the debugging chapter at the end of the topic.
Learning to debug issues properly is absolutely worth investing the time in be-
cause it'll quickly pay back in improved productivity, which is much better than
banging your head for days over a seemingly inexplicable issue. I'll shut up
about it until then, I promise, but please do yourself the favor and check it out.
Looking at Listing 3-9 , it seems rather unnecessary to search through the entire node hier-
archy starting with self when you know that the background and physics nodes are chil-
dren of the _levelNode , and the player is a child of the physics node. A slightly better
(but entirely optional) version of the preceding code is shown in Listing 3-10 .
Listing 3-10 . This version of loadLevelNamed: avoids searching recursively where it isn't
necessary
-(void) loadLevelNamed:(NSString*)levelCCB
{
_physicsNode = (CCPhysicsNode*)[_levelNode
getChildByName:@"physics" recursively:NO];
_backgroundNode = [_levelNode
getChildByName:@"background" recursively:NO];
Search WWH ::




Custom Search