Game Development Reference
In-Depth Information
{
__weak CCNode* _levelNode;
__weak CCPhysicsNode* _physicsNode;
__weak CCNode* _playerNode;
__weak CCNode* _backgroundNode;
__weak GameMenuLayer* _gameMenuLayer;
__weak GameMenuLayer* _popoverMenuLayer;
// Other ivars omitted for brevity...
}
A lot can go wrong here, and it's quite common to forget to assign the
doc root var
in
SpriteBuilder or to make a typo in the node's name. Or you might assign the
doc root var
to the wrong node or in the wrong CCB file, or you might edit a node's name in the
Timeline rather than its name property. It is good style to verify the assumption that these
variables have to be non-
nil
after a given point during the initialization of the class.
good place to assert that crucial ivars are non-
nil
.
Listing 14-7
.
Asserting that specific ivars are non-nil after the level was loaded
-(void) loadLevelNamed:(NSString*)levelCCB
{
// Existing code omitted for brevity...
NSAssert1(_physicsNode, @"physics node not found in
level: %@", levelCCB);
NSAssert1(_backgroundNode, @"bg node not found in
level: %@", levelCCB);
NSAssert1(_playerNode, @"player node not found in
level: %@", levelCCB);
}
To test a pointer variable for being non-
nil
, it suffices to just write the ivar's name. In a
condition or assertion, writing
_playerNode
is equal to writing
_playerNode != nil
.
You can also use the same format strings as in
NSLog
; however, with
NSAssert
being a
macro and not an actual C function, you have to use
NSAssert1
through
NSAssert5
to match the number of format-string parameters. In the preceding example, there's one
format-string parameter,
levelCCB
, so
NSAssert1
needed to be used.
Search WWH ::
Custom Search