Game Development Reference
In-Depth Information
Moving the clouds
Our next enhancement will be to make clouds, which are just sitting in the air and
move to the left. The idea here is to simulate some kind of wind property in the game
world, while each cloud will also move at slightly different speeds to make their
movement look more natural. In addition to this, there needs to be a condition to let
more clouds appear at the right end of the level. Otherwise, the game will run out
of clouds at some point. We could just take the easy route here and spawn a huge
number, say 1000 clouds, but this is not very clever performance-wise, and this also
does not tackle our original problem, that is, the game will run out of clouds at some
point. So, a better approach to solve our problem is to keep the maximum number of
existing cloud objects to a minimum.
Make the following changes to the Clouds class:
private Cloud spawnCloud () {
Cloud cloud = new Cloud();
cloud.dimension.set(dimension);
// select random cloud image
cloud.setRegion(regClouds.random());
// position
Vector2 pos = new Vector2();
pos.x = length + 10; // position after end of level
pos.y += 1.75; // base position
// random additional position
pos.y += MathUtils.random(0.0f, 0.2f)
* (MathUtils.randomBoolean() ? 1 : -1);
cloud.position.set(pos);
// speed
Vector2 speed = new Vector2();
speed.x += 0.5f; // base speed
// random additional speed
speed.x += MathUtils.random(0.0f, 0.75f);
cloud.terminalVelocity.set(speed);
speed.x *= -1; // move left
cloud.velocity.set(speed);
return cloud;
}
Next, add the following lines of code to the same class:
@Override
public void update (float deltaTime) {
for (int i = clouds.size - 1; i>= 0; i--) {
Cloud cloud = clouds.get(i);
 
Search WWH ::




Custom Search