Game Development Reference
In-Depth Information
The
World
class will then keep track of multiple instances of these objects, update them each
frame, check collisions between objects and Bob, and carry out the collision responses (that is,
let Bob die, collect a coin, and so forth). We will go through each class next, from simplest to
most complex.
The Spring Class
Let's start with the
Spring
class, presented in Listing 9-9.
Listing 9-9. Spring.java, the Spring Class
package
com.badlogic.androidgames.jumper;
import
com.badlogic.androidgames.framework.GameObject;
public class
Spring
extends
GameObject {
public static float
SPRING
_
WIDTH
= 0.3f;
public static float
SPRING
_
HEIGHT
= 0.3f;
public
Spring(
float
x,
float
y) {
super
(x, y, SPRING_WIDTH, SPRING_HEIGHT);
}
}
The
Spring
class derives from the
GameObject
class that we created in Chapter 8. We only need
a position and a bounding shape since a spring does not move.
Next, we define two constants that are publicly accessible: the spring width and the spring
height, in meters. We estimated those values previously, and we just reuse them here.
The final piece is the constructor, which takes the x and y coordinates of the spring's center.
With this, we call the constructor of the superclass
GameObject
, which takes the position as well
as the width and height of the object from which to construct a bounding shape (a
Rectangle
centered around the given position). With this information, our
Spring
class is fully defined,
having a position and bounding shape against which to collide.
The Coin Class
Next up is the class for coins, shown in Listing 9-10.
Listing 9-10. Coin.java, the Coin Class
package
com.badlogic.androidgames.jumper;
import
com.badlogic.androidgames.framework.GameObject;
public class
Coin
extends
GameObject {
public static final float
COIN
_
WIDTH
= 0.5f;
public static final float
COIN
_
HEIGHT
= 0.8f;
public static final int
COIN
_
SCORE
= 10;