Game Development Reference
In-Depth Information
if (self.position.x > outsideScreen)
{
[self removeFromParentAndCleanup:YES];
}
}
The bullet position is updated by multiplying its velocity (speed and direction given by
a CGPoint ) and then adding the result to the bullet's position. The velocity simply de-
termines how many pixels to move in each direction every second. Multiplying the ve-
locity by the time gives the distance the bullet traveled. The reason for using the delta
time when updating the position is that this makes the movement of the bullets inde-
pendent from the framerate. If you don't do that for all moving objects, your game
would slow down proportionally with a decreasing framerate—for example, when
there's a boss fight with lots of sprites on the screen.
Calculating the movement in the aptly named update method update is much more
effective than using CCMoveTo or CCMoveBy actions in this case. It avoids some
overhead and the problem that actions run for a given duration. If the ship were to
move closer to the right-hand side of the screen, the move actions would cause the bul-
lets to move more slowly because they need to travel a shorter distance in the same
time.
Tip You can have the CCMoveTo and CCMoveBy actions move a node at a
fixed speed to any position. To do so, you would first have to calculate the dis-
tance between the node's current position to its desired destination by using the
ccpDistance method. Then divide the distance by the desired speed (in
pixels per frame). This works well enough, but the drawback is that
ccpDistance calls the sqrtf (square root) method, which is computation-
ally expensive. You should avoid doing this regularly. For something as simple
as updating node positions every frame, avoid using move actions for continu-
ously moving nodes.
Introducing the CCSpriteBatchNode
In the Sprites01 project, I decided to add the CCSpriteBatchNode for bullets to the
GameScene itself, because bullets aren't supposed to be added to the Ship class. Be-
Search Nedrilad ::




Custom Search