Game Development Reference
In-Depth Information
float
stateTime;
public
Coin(
float
x,
float
y) {
super
(x, y,
COIN
_
WIDTH
,
COIN
_
HEIGHT
);
stateTime = 0;
}
public void
update(
float
deltaTime) {
stateTime += deltaTime;
}
}
The
Coin
class is pretty much the same as the
Spring
class, with only one difference: we keep
track of the duration the coin has been alive already. This information is needed when we want to
render the coin later on using an
Animation
. We did the same thing for our cavemen in the final
example of Chapter 8. It is a technique we'll use for all our simulation classes. Given a state and
a state time, we can select an
Animation
, as well as the keyframe of that
Animation
, to use for
rendering. The coin only has a single state, so we only need to keep track of the state time. For
that we have the
update()
method, which will increase the state time by the delta time passed to it.
The constants defined at the top of the class specify a coin's width and height, as previously
defined, as well as the number of points Bob earns if he hits a coin.
The Castle Class
Next up, we have a class for the castle at the top of our world. Listing 9-11 shows the code.
Listing 9-11. Castle.java, the Castle Class
package
com.badlogic.androidgames.jumper;
import
com.badlogic.androidgames.framework.GameObject;
public class
Castle
extends
GameObject {
public static float
CASTLE
_
WIDTH
= 1.7f;
public static float
CASTLE
_
HEIGHT
= 1.7f;
public
Castle(
float
x,
float
y) {
super
(x, y, CASTLE_WIDTH, CASTLE_HEIGHT);
}
}
Not too complex. All we need to store is the position and bounds of the castle. The size of
a castle is defined by the constants
CASTLE_WIDTH
and
CASTLE_HEIGHT
, using the values we
discussed earlier.
The Squirrel Class
Next is the
Squirrel
class, shown in Listing 9-12.