Game Development Reference
In-Depth Information
The Rock class has two variables, regEdge and regMiddle , to store the
corresponding texture regions for the edge and the middle part of a rock.
Additionally, there is a length variable that describes the number of middle parts
to use for a rock, or in other words, the resulting length of the rock. In the init()
method, we set the dimension, the width, and the height of the rock. Remember
that these values are given in meters, which relate to our game world. So, in this
case, a rock is 1 meter wide and 1.5 meters tall. Next, the texture regions are stored
in local variables. Obviously, this is not a necessary step and is really just for our
convenience to allow quick and easy changes to the texture regions the code will
use to render a specific part. Finally, setLength() is called to set the starting length
of the rock. The increaseLength() method allows you to increase the length of
the rock by a given amount. It will come in handy later on when our yet-to-be-
implemented level loader eventually creates these rocks.
As Rock inherits from AbstractGameObject , it is mandatory to also implement its
render() method. Add the following code to the Rock class:
@Override
public void render (SpriteBatch batch) {
TextureRegion reg = null;
float relX = 0;
float relY = 0;
// Draw left edge
reg = regEdge;
relX -= dimension.x / 4;
batch.draw(reg.getTexture(), position.x + relX, position.y +
relY, origin.x, origin.y, dimension.x / 4, dimension.y,
scale.x, scale.y, rotation, reg.getRegionX(), reg.getRegionY(),
reg.getRegionWidth(), reg.getRegionHeight(), false, false);
// Draw middle
relX = 0;
reg = regMiddle;
for (int i = 0; i < length; i++) {
batch.draw(reg.getTexture(), position.x + relX, position.y
+ relY, origin.x, origin.y, dimension.x, dimension.y,
scale.x, scale.y, rotation, reg.getRegionX(), reg.getRegionY(),
reg.getRegionWidth(), reg.getRegionHeight(), false, false);
relX += dimension.x;
}
// Draw right edge
 
Search WWH ::




Custom Search