Game Development Reference
In-Depth Information
public Caveman( float x, float y, float width, float height) {
super (x, y, width, height);
this .position.set(( float )Math. random () * WORLD_WIDTH ,
( float )Math. random () * WORLD_HEIGHT );
this .velocity.set(Math. random () > 0.5f?-0.5f:0.5f, 0);
this .walkingTime = ( float )Math. random () * 10;
}
public void update( float deltaTime) {
position.add(velocity.x * deltaTime, velocity.y * deltaTime);
if (position.x < 0) position.x = WORLD_WIDTH ;
if (position.x > WORLD_WIDTH ) position.x = 0;
walkingTime += deltaTime;
}
}
The two constants WORLD_WIDTH and WORLD_HEIGHT are part of the enclosing AnimationTest class,
and are used by the inner classes. Our world is 4.8×3.2 m in size.
Next up is the inner Caveman class, which extends DynamicGameObject , since we will move
cavemen based on velocity. We define an additional member that keeps track of how long the
caveman has been walking. In the constructor, we place the caveman at a random position and
let him walk to the left or the right. We initialize the walkingTime member to a number between 0
and 10; this way our cavemen won't walk in sync.
The update() method advances the caveman based on his velocity and the delta time. If he
leaves the world, we reset him to either the left or right edge. We add the delta time to the
walkingTime to keep track of how long he's been walking.
Listing 8-21 shows the AnimationScreen class.
Listing 8-21. Excerpt from AnimationTest.java; the AnimationScreen Class
class AnimationScreen extends Screen {
static final int NUM_CAVEMEN = 10;
GLGraphics glGraphics;
Caveman[] cavemen;
SpriteBatcher batcher;
Camera2D camera;
Texture texture;
Animation walkAnim;
Our screen class has the usual suspects as members. We have a GLGraphics instance, a Caveman
array, a SpriteBatcher , a Camera2D , the Texture containing the walking keyframes, and an
Animation instance.
public AnimationScreen(Game game) {
super (game);
glGraphics = ((GLGame)game).getGLGraphics();
cavemen = new Caveman[ NUM_CAVEMEN ];
for ( int i = 0; i < NUM_CAVEMEN ; i++) {
cavemen[i] = new Caveman(( float )Math. random (), ( float )Math. random (), 1, 1);
}
 
Search WWH ::




Custom Search