Game Development Reference
In-Depth Information
Assigning a variable via SpriteBuilder and obtaining a node by name are equally fine. For
the most part, it's a matter of personal preference, though it's recommended not to use the
getChildByName: method frequently in a scheduled method or in update: meth-
ods, in particular with recursive searches and a deep-node hierarchy.
Caution At the time of this writing, assigning variables from within
SpriteBuilder works only for nodes that are direct descendants of a given CCB
file. It cannot be used to assign nodes to ivars or properties where the node is in
another CCB imported via a Sub File (CCBFile) node. This is the reason why
the player node was obtained by name.
Open the GameScene.ccb within SpriteBuilder, and select the CCBFile instance that refer-
ences the Levels/Level1.ccb. Then, on the Details Inspector on the right side, switch to the
Item Code Connections tab. In the second row's text field, enter _levelNode , which is the
name of the ivar you already declared in Listing 3-1 . Do leave the drop-down menu set to
“Doc root var” as shown in Figure 3-2 .
Figure 3-2 . A doc root var assigns a node to a correspondingly named ivar or property declared in the CCB
root node's custom class
If you publish, build, and run, the _levelNode ivar will be assigned by CCBReader
before it sends the didLoadFromCCB message. That's the easiest, most efficient way to
create a reference to a node contained within a CCB.
If you want to confirm that the variable has been assigned, you could add this log state-
ment to didLoadFromCCB :
NSLog(@"_levelNode = %@", _levelNode);
Run the project and you'll find the preceding line logged in the Xcode Debug Console. If
necessary, open the Console in Xcode under View Debug Area Activate Console.
If you see _levelNode = (null) , the variable name in Figure 3-2 probably doesn't
match the ivar's name. Otherwise, the message in the log will be similar to this:
Search WWH ::




Custom Search