Game Development Reference
In-Depth Information
Let's look at the first case. If the rocket is waiting to be spawned, you simply subtract the time that
has elapsed since the last
update
call from the spawn time:
if (this.spawnTime > 0) {
this.spawnTime -= delta;
return;
}
The second case is slightly more complicated. The rocket is moving from one end of the screen to
the other. So, you set the visibility status to
true
, you calculate the rocket velocity depending on the
direction it's moving, and you update its position:
this.visible = true;
this.velocity.x = 600;
if (this.mirror)
this.velocity.x *= -1;
Finally, you have to check whether the rocket has flown outside of the screen. If that is the case, the
rocket should be reset. You check whether the rocket is outside of the screen using bounding boxes.
If the bounding box enclosing the screen doesn't intersect the rocket's bounding box, you know the
rocket is outside of the screen, and you reset it:
var screenBox = new powerupjs.Rectangle(0, 0, powerupjs.Game.size.x, powerupjs.Game.size.y);
if (!screenBox.intersects(this.boundingBox))
this.reset();
This completes the
Rocket
class, except for interaction with the player, which is something you look
at in more detail in the following chapter. For the complete class, see the
TickTick3
example code
belonging to this chapter. Figure
27-1
shows a screenshot of the level defined in the first section of
this chapter.
Search WWH ::
Custom Search