Game Development Reference
In-Depth Information
For code hot swapping to work, it is necessary that the automatic (re)build feature
is enabled. You can quickly check this by going to the Project menu and making
sure that the menu item Build Automatically is checked.
You might already sense the possibilities that this great feature enables a developer
to do. Just think of a somewhat more complex scene where you are trying to find
the best-looking positions for your objects, or you just want to see how it would
look with a couple of different settings. It's a piece of cake with a tool like code
hot swapping at your disposal.
Let's take the preceding example a bit further and make the image rotate continuously.
We will need a variable to store the current rotation value. This value is going to be
increased over a period of time. To avoid a possible overflow in rot , we calculate
the remainder of the new rotation value divided by 360 degrees. This can be done in
an easy way using the modulo operator ( % ) to wrap around a certain value.
The rotation is calculated in degrees per second. Afterwards, we set the new rotation
value of the sprite and draw it while the rotation value is advanced step by step.
The following listing is the modified code for the rotating image:
private float rot;
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
final float degreesPerSecond = 10.0f;
rot = (rot + Gdx.graphics.getDeltaTime() *
degreesPerSecond) % 360;
sprite.setRotation(rot);
sprite.draw(batch);
batch.end();
}
 
Search WWH ::




Custom Search