Game Development Reference
In-Depth Information
dimension.set(3.0f, 1.5f);
regClouds = new Array<TextureRegion>();
regClouds.add(Assets.instance.levelDecoration.cloud01);
regClouds.add(Assets.instance.levelDecoration.cloud02);
regClouds.add(Assets.instance.levelDecoration.cloud03);
int distFac = 5;
int numClouds = (int)(length / distFac);
clouds = new Array<Cloud>(2 * numClouds);
for (int i = 0; i < numClouds; i++) {
Cloud cloud = spawnCloud();
cloud.position.x = i * distFac;
clouds.add(cloud);
}
}
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
pos.y += MathUtils.random(0.0f, 0.2f)
* (MathUtils.randomBoolean() ? 1 : -1); // random
additional position
cloud.position.set(pos);
return cloud;
}
@Override
public void render (SpriteBatch batch) {
for (Cloud cloud : clouds)
cloud.render(batch);
}
}
The Clouds class is also constructed like the previous game objects. The distribution
of the clouds over the level is determined by the given length value and the constant
factor distFact , which is 5 in this code, meaning that there will be a cloud every
five meters.
 
Search WWH ::




Custom Search