Game Development Reference
In-Depth Information
pixmap.setColor(1, 0, 0, 0.5f);
pixmap.fill();
// Draw a yellow-colored X shape on square
pixmap.setColor(1, 1, 0, 1);
pixmap.drawLine(0, 0, width, height);
pixmap.drawLine(width, 0, 0, height);
// Draw a cyan-colored border around square
pixmap.setColor(0, 1, 1, 1);
pixmap.drawRectangle(0, 0, width, height);
return pixmap;
}
public void update (float deltaTime) {
updateTestObjects(deltaTime);
}
private void updateTestObjects(float deltaTime) {
// Get current rotation from selected sprite
float rotation = testSprites[selectedSprite].getRotation();
// Rotate sprite by 90 degrees per second
rotation += 90 * deltaTime;
// Wrap around at 360 degrees
rotation %= 360;
// Set new rotation value to selected sprite
testSprites[selectedSprite].setRotation(rotation);
}
}
The new code adds two new member variables,
testSprites
and
selectedSprite
.
The first one holds instances of the
Sprite
objects. We chose to add five sprites for
our test. The second variable holds the index of the currently selected sprite that is
stored in the array. The
Sprite
class can be used to display textures. As we do not
have any textures added to our project yet, we will generate one for our test on the
fly using the
Pixmap
class.
Pixmap
holds the actual pixel data (in a map of bytes) to
represent any image. Its class provides some basic drawing methods that we will
use in this code to draw a 32 x 32 pixel-sized transparent red box with a yellow "X"
crossing it diagonally and a cyan border. The final pixel data is then put in a new
Texture
object. This object is eventually attached to each new
Sprite
we create so
that it will show our handcrafted image when rendered.