Game Development Reference
In-Depth Information
does not come into effect for bodies that leave the screen and continue to fall forever in a
big empty void.
Interlude: Dealing with Lost Bodies
Cocos2D will never remove nodes automatically. Nodes not currently visible on screen
are still part of the scene hierarchy; they remain in memory and perform actions though
they will not be rendered.
If you were to program a game where nodes are created but never deleted as they move
outside the playable area, eventually the game's frame rate is going to decrease over time.
In such instances, you have to have code in place that checks periodically whether each
node's position has crossed an imaginary boundary—for instance, in the update: meth-
od. If a node is determined to be outside the allowed boundary, that node is sent the re-
moveFromParent message in order to remove it. An example is given in Listing 4-6 .
Listing 4-6 . Example for removing a node based on its Y coordinate
-(void) update:(CCTime)delta
{
if (_playerNode.position.y <
(-_playerNode.contentSize.height))
{
[_playerNode removeFromParent];
}
// more code here ...
}
The (-_playerNode.contentSize.height) is in brackets only for clarity be-
cause there's an easily overlooked minus sign in front of it.
In this project you don't need a check like in Listing 4-6 , because you'll enclose the level
with impenetrable walls.
Creating Static Level Borders
You may remember from earlier in this chapter that physics bodies have two modes: Dy-
namic and Static . Dynamic allows a body to move freely about the world, and it will
Search WWH ::




Custom Search