Game Development Reference
In-Depth Information
After this, make the following changes to the same class:
public void update (float deltaTime) {
stateTime += deltaTime;
if (body == null) {
updateMotionX(deltaTime);
updateMotionY(deltaTime);
// Move to new position
position.x += velocity.x * deltaTime;
position.y += velocity.y * deltaTime;
} else {
position.set(body.getPosition());
rotation = body.getAngle() * MathUtils.radiansToDegrees;
}
}
With these additions, we introduce two new common attributes that are shared with
every game object in Canyon Bunny, the state time ( stateTime ) and the currently set
animation ( animation ), which are going to be used for rendering the game object.
We also added a convenience method called setAnimation() that allows you to
change the current animation as well as reset the state time to 0 . This is desirable
because in almost every case, we do not want to start somewhere in the middle of
a new animation but instead we want to start right from the beginning at the first
frame. The change in the update() method simply makes sure that the state time is
increased, which allows the animation to run.
Now, add the following import line to the GoldCoin class:
import com.badlogic.gdx.math.MathUtils;
After this, make the following changes to the same class:
private void init () {
dimension.set(0.5f, 0.5f);
setAnimation(Assets.instance.goldCoin.animGoldCoin);
stateTime = MathUtils.random(0.0f, 1.0f);
// Set bounding box for collision detection
bounds.set(0, 0, dimension.x, dimension.y);
collected = false;
 
Search WWH ::




Custom Search