Game Development Reference
In-Depth Information
The first line allows the GameScene class to receive touch events. Add the Listing 3-3
code shown next to the didLoadFromCCB method. It will be expanded upon later in the
topic; it doesn't currently load levels yet.
Listing 3-3 . Getting the player node by its name
-(void) loadLevelNamed:(NSString*)levelCCB
{
// get the current level's player in the scene by
searching for it recursively
_playerNode = [self getChildByName:@"player"
recursively:YES];
NSAssert1(_playerNode, @"player node not found in
level: %@", levelCCB);
}
Here the first child node that goes by the name “player” is returned. The search is per-
formed recursively, which means search covers the entire node hierarchy, including all
child and grandchild nodes of all children in the self node. The result is assigned to the
_playerNode ivar.
If the _playerNode couldn't be found by its name, the NSAssert here will throw an
exception and spit out a message in the log. Safety measures like these are good practice
because they allow you to catch issues the moment something doesn't appear to be as ex-
pected, rather than leaving you wondering why the player won't move. For the rest of the
topic, I'll leave out the safety checks for brevity and readability, but you can learn more
about debugging strategies and defensive programming techniques in this topic's chapter
14 : Debugging and Best Practices.
There's but one thing that you ought to do in every Xcode project: add an exception
breakpoint as shown in Figure 3-1 . To do this, click on the Breakpoint Navigator tab in
Xcode (the right-facing arrow-like icon highlighted in tab bar). At the bottom of the pane
is an Add button (a + symbol). Click it and choose Add Exception Breakpoint. You can
optionally edit the breakpoint by right-clicking it. In Figure 3-1 , it is set to catch
Objective-C exceptions only. The exception breakpoint will halt the program at the of-
fending line of code so that you know exactly where a program error occurred, rather than
Xcode always jumping to the main method. Exception breakpoints are an essential de-
bugging aid.
Search WWH ::




Custom Search