Game Development Reference
In-Depth Information
for (int i = 0; i < testSprites.length; i++) {
Sprite spr = new Sprite(regions.random());
// Define sprite size to be 1m x 1m in game world
spr.setSize(1, 1);
// Set origin to sprite's center
spr.setOrigin(spr.getWidth() / 2.0f,
spr.getHeight() / 2.0f);
// Calculate random position for sprite
float randomX = MathUtils.random(-2.0f, 2.0f);
float randomY = MathUtils.random(-2.0f, 2.0f);
spr.setPosition(randomX, randomY);
// Put new sprite into array
testSprites[i] = spr;
}
// Set first sprite as selected one
selectedSprite = 0;
}
We are still creating five sprites in initTestObjects() as before. The difference is
that we are now using the bunny head, feather, and gold coin texture regions. To
make our test a little bit more interesting, we create a dynamic array called regions
that holds all the texture regions we want to use. Then, inside the setup loop, we
create a Sprite instance with a randomly picked texture region from the dynamic
array. This results in a random number of similar looking objects in the test scene.
You can now run the game to test it out. The following is a screenshot that shows a
possible outcome due to the randomness that we built in:
 
Search WWH ::




Custom Search