Game Development Reference
In-Depth Information
batcher =
new
SpriteBatcher(glGraphics,
NUM_CAVEMEN
);
camera =
new
Camera2D(glGraphics,
WORLD_WIDTH
,
WORLD_HEIGHT
);
}
In the constructor, you create the
Caveman
instances, as well as the
SpriteBatcher
and
Camera2D
.
@Override
public void
resume() {
texture =
new
Texture(((GLGame)game), "walkanim.png");
walkAnim =
new
Animation( 0.2f,
new
TextureRegion(texture, 0, 0, 64, 64),
new
TextureRegion(texture, 64, 0, 64, 64),
new
TextureRegion(texture, 128, 0, 64, 64),
new
TextureRegion(texture, 192, 0, 64, 64));
}
In the
resume()
method, we load the texture atlas containing the animation keyframes from
the asset file
walkanim.png
, which is the same as seen in Figure
8-25
. Afterward, we create the
Animation
instance, setting the frame duration to 0.2 s and passing in a
TextureRegion
for each
of the keyframes in the texture atlas.
@Override
public void
update(
float
deltaTime) {
int
len = cavemen.length;
for
(
int
i = 0; i < len; i++) {
cavemen[i].update(deltaTime);
}
}
The
update()
method just loops over all
Caveman
instances and calls their
Caveman.update()
method with the current delta time. This will make the cavemen move and will update their
walking times.
@Override
public void
present(
float
deltaTime) {
GL10 gl = glGraphics.getGL();
gl.glClear(GL10.
GL_COLOR_BUFFER_BIT
);
camera.setViewportAndMatrices();
gl.glEnable(GL10.
GL_BLEND
);
gl.glBlendFunc(GL10.
GL_SRC_ALPHA
, GL10.
GL_ONE_MINUS_SRC_ALPHA
);
gl.glEnable(GL10.
GL_TEXTURE_2D
);
batcher.beginBatch(texture);
int
len = cavemen.length;
for
(
int
i = 0; i < len; i++) {
Caveman caveman = cavemen[i];
TextureRegion keyFrame = walkAnim.getKeyFrame(caveman.walkingTime,
Animation.
ANIMATION_LOOPING
);
batcher.drawSprite(caveman.position.x, caveman.position.y,
caveman.velocity.x < 0?1:-1, 1, keyFrame);
}