Game Development Reference
In-Depth Information
Note that some changes cannot be hot swapped into a running
application, such as changing method names and introducing
new variables in class. In order to reflect these changes, you have
to rerun the program. However, in such situations, Eclipse will
issue a warning when the code cannot be hot swapped.
Now that we have a changing value for the rotation, let's have some more fun with it
and turn the continuous rotation effect into a shake effect.
As the sine (or cosine) function has an oscillating behavior, we can make perfect use
of it to make the image shake by a certain amount to the left and right. The amount
(amplitude) can be increased and decreased by multiplying it with the answer of
the sine function.
The following listing is the modified code for the shaking image:
@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();
float degreesPerSecond = 10.0f;
rot = (rot + Gdx.graphics.getDeltaTime() *
degreesPerSecond) % 360;
final float shakeAmplitudeInDegrees = 5.0f;
float shake = MathUtils.sin(rot) * shakeAmplitudeInDegrees;
sprite.setRotation(shake);
sprite.draw(batch);
batch.end();
}
 
Search WWH ::




Custom Search