Game Development Reference
In-Depth Information
b2debugRenderer = new Box2DDebugRenderer();
}
private void renderWorld (SpriteBatch batch) {
worldController.cameraHelper.applyTo(camera);
batch.setProjectionMatrix(camera.combined);
batch.begin();
worldController.level.render(batch);
batch.end();
if (DEBUG_DRAW_BOX2D_WORLD) {
b2debugRenderer.render(worldController.b2world,
camera.combined);
}
}
Now, we can easily toggle the debug view for Box2D, by setting
DEBUG_DRAW_
BOX2D_WORLD
to
true
or
false
.
Next, add the following lines of code to the
Constants
class:
// Number of carrots to spawn
public static final int CARROTS_SPAWN_MAX = 100;
// Spawn radius for carrots
public static final float CARROTS_SPAWN_RADIUS = 3.5f;
// Delay after game finished
public static final float TIME_DELAY_GAME_FINISHED = 6;
We will use these new constants to control the number of carrots to spawn and the
delay time before the game switches back to the menu screen after the goal was
reached by the player. In this particular case, we use a value of 100 carrots and a
delay of 6 seconds.
Next, add the following lines of code to the
WorldController
class:
private void spawnCarrots (Vector2 pos, int numCarrots,
float radius) {
float carrotShapeScale = 0.5f;
// create carrots with box2d body and fixture
for (int i = 0; i<numCarrots; i++) {
Carrot carrot = new Carrot();
// calculate random spawn position, rotation, and scale
float x = MathUtils.random(-radius, radius);