Game Development Reference
In-Depth Information
Time for action - adding the main loop
Let's implement our main
update
method.
1. In
GameLayer.cpp
, inside the
update
method, add the following lines:
if (!_running || _state != kGamePlay) return;
if (_lineContainer->getLineType() != LINE_NONE) {
_lineContainer->setTip (_rocket->getPosition() );
}
if (_rocket->collidedWithSides()) {
_lineContainer->setLineType ( LINE_NONE );
}
_rocket->update(dt);
//update jet particle so it follows rocket
if (!_jet->isActive()) _jet->resetSystem();
_jet->setRotation(_rocket->getRotation());
_jet->setPosition(_rocket->getPosition());
We check to see if we are not currently on pause. Then, if there is a line for our
ship that we need to show in
_lineContainer
, we update the line's
tip
point
with the
_rocket
current position.
We run collision checks between
_rocket
and the screen sides, update the
_rocket
sprite, and position and rotate our
_jet
particle system to align it with
the
_rocket
sprite.
2. Next we update
_comet
(its countdown, initial position, movement, and collision
with
_rocket
if
_comet
is visible):
_cometTimer += dt;
float newY;
if (_cometTimer > _cometInterval) {
_cometTimer = 0;
if (_comet->isVisible() == false) {
_comet->setPositionX(0);