Game Development Reference
In-Depth Information
cloud.update(deltaTime);
if (cloud.position.x< -10) {
// cloud moved outside of world.
// destroy and spawn new cloud at end of level.
clouds.removeIndex(i);
clouds.add(spawnCloud());
}
}
}
The spawnCloud() method will now create a new cloud that also makes use of our
simple physics simulation code we implemented earlier. The update() method
iterates through all existing clouds, which in turn calls the update() method to
let the physics move them. Afterwards, the cloud's new position is checked to see
whether it has moved off screen. If a cloud fulfills this condition, it is removed from
the list of current clouds and a new one is added and positioned at the right end of
the level.
The list of clouds in update() is iterated in reverse on purpose to avoid
a so-called mutating list. Normally, you should never modify the list you
are currently iterating over. However, when iterating backwards from
the last to the first element, the removal of an object, like in our case, will
only happen to elements that have already been processed.
Smoothing with linear interpolation
(Lerp)
Lerp is a method to find unknown values between two known points. The unknown
values are approximated through Lerp by connecting these two known points with a
straight line.
Lerp operations can also be used to smoothen movements. We will show this using
an example in which we will smoothen the camera's target-following feature as well
as use it to make the rocks move up and down slightly to simulate them floating on
the water. First, add the following line to the CameraHelper class:
private final float FOLLOW_SPEED = 4.0f;
 
Search WWH ::




Custom Search