Game Development Reference
In-Depth Information
// Add the bullets to the ship's parent
CCNode* gameScene = [self parent];
[gameScene addChild:bullet z:0 tag:GameSceneNodeTagBullet];
}
The Bullet sprites are added to the ship's parent for the simple reason that adding
them to the ship would position all flying bullets at an offset to the ship. This means
that if the ship were to move—if it didn't, this would be a really boring game—all the
flying bullets would change their positions relative to the ship, as if they were some-
how attached to it.
Tip The bullets are all added with the same z-order, 0. All nodes using the
same z-order are drawn in the order in which they're added to the scene hier-
archy, meaning the node added last will be drawn in front of all other previ-
ously added nodes with the same z-order.
In addition, all bullets use the same tag. Tags don't need to be unique, and
sometimes it can be helpful to use a tag to denote group membership of nodes.
You could then loop through all children of a node and perform different code
based on the node tag.
The bullets also use an update method to update their position and to remove them-
selves at some point. Although sprites aren't drawn if they're outside the screen, they
still consume memory and CPU power, so it's good practice to remove any stray ob-
jects that leave the screen area at some point in time. In this case, you simply check the
bullet's position against the right-hand side of the screen, as shown in Listing 6-5 .
Listing 6-5. Moving and Removing the Bullets
-(void) update:(ccTime)delta
{
// update position of the bullet
// multiply the velocity by the time since the last update was called
// this ensures same bullet velocity even if framerate drops
self.position = ccpAdd(self.position, ccpMult(velocity, delta));
// delete the bullet if it leaves the screen
 
 
Search Nedrilad ::




Custom Search