Game Development Reference
In-Depth Information
Create a new file for the Goal class and add the following code:
package com.packtpub.libgdx.canyonbunny.game.objects;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.packtpub.libgdx.canyonbunny.game.Assets;
public class Goal extends AbstractGameObject {
private TextureRegion regGoal;
public Goal () {
init();
}
private void init () {
dimension.set(3.0f, 3.0f);
regGoal = Assets.instance.levelDecoration.goal;
// Set bounding box for collision detection
bounds.set(1, Float.MIN_VALUE, 10, Float.MAX_VALUE);
origin.set(dimension.x / 2.0f, 0.0f);
}
public void render (SpriteBatch batch) {
TextureRegion reg = null;
reg = regGoal;
batch.draw(reg.getTexture(), position.x - origin.x,
position.y - origin.y, origin.x, origin.y, dimension.x,
dimension.y, scale.x, scale.y, rotation,
reg.getRegionX(), reg.getRegionY(),
reg.getRegionWidth(), reg.getRegionHeight(),
false, false);
}
}
There is one specialty about this game object that is important enough to be mentioned.
We have set the bounds of the goal to values that make it almost infinitely tall in
relation to other objects in the game world. This ensures that the player character will
always collide with the goal and trigger the corresponding event.
 
Search WWH ::




Custom Search