Daniel G. Wilson
Daniel G. Wilson

Reputation: 15055

The Functionality of CCNode.children - Is this Redundant Code?

In a rudimentary hit test function of a template I found for a cocos2d game, the original author used the following to determine which objects to preform the hit test on during the run loop.

for (Enemy *someEnemy in self.children) {

    if ( [someEnemy isKindOfClass:[Enemy class]] ) {
    ...
    }
}

Could someone elaborate on the idiosyncrasies of the list returned by .children? More specifically, is the if statement in the above code actually necessary?

Upvotes: 0

Views: 30

Answers (1)

Bivis
Bivis

Reputation: 1407

The .children will return an array of all nodes that added as child on node.

if you add 4 children on self

 [self addChild:node1];
 [self addChild:node2];
 [self addChild:node3];
 [self addChild:node4];

then children will return these 4 nodes

 NSLog(@"children: %@", self.children);

... Your console will show the 4 children

Upvotes: 1

Related Questions