Beginning Java Game Development with LibGDX by Lee Stemkoski

Beginning Java Game Development with LibGDX by Lee Stemkoski

Author:Lee Stemkoski
Language: eng
Format: epub, pdf
Publisher: Apress, Berkeley, CA


Rocks and Explosions

Next, it is time to move on to the rocks of the Space Rocks game. There does not need to be a base version of the object to clone later, since rocks are destroyed when hit by lasers, and no new rocks spawn at a later time.1 For simplicity, you could still create a base version and clone it repeatedly to produce the set of rocks drifting around the screen at the start of the game. However, you will instead attempt to make the individual rocks appear and act differently to add interest to the game. In particular, the rocks will use different images (the file names are rock0.png, rock1.png, rock2.png, and rock3.png), the initial positions will be random, and they will have different speeds and rates of rotation. Here, you must also initialize the ArrayList being used to keep track of the rocks for collision detection. The code that accomplishes this is given here:

rockList = new ArrayList<PhysicsActor>();

int numRocks = 6;

for (int n = 0; n < numRocks; n++)

{

PhysicsActor rock = new PhysicsActor();

String fileName = "assets/rock" + (n%4) + ".png";

Texture rockTex = new Texture(Gdx.files.internal(fileName));

rockTex.setFilter(TextureFilter.Linear, TextureFilter.Linear);

rock.storeAnimation( "default", rockTex );

rock.setPosition(800 * MathUtils.random(), 600 * MathUtils.random() );

rock.setOriginCenter();

rock.setEllipseBoundary();

rock.setAutoAngle(false);

float speedUp = MathUtils.random(0.0f, 1.0f);

rock.setVelocityAS( 360 * MathUtils.random(), 75 + 50*speedUp );

rock.addAction( Actions.forever( Actions.rotateBy(360, 2 - speedUp) ) );

mainStage.addActor(rock);

rockList.add(rock);

rock.setParentList(rockList);

}

In the update method of the GameScreen class, some code must be added that causes the rocks to wrap around the screen in the same style as the spaceship:

for ( PhysicsActor rock : rockList )

{

wraparound( rock );

}

This is another good point to compile your project and run the game to make sure that the rock objects are behaving as expected.

Next, you’ll set up the AnimatedActor that stores an animated explosion that will appear when lasers collide with rocks. For animation sequences consisting of many images, it is common practice to combine all these images into a single image file called a sprite sheet, and this is the case for the image you will use, pictured in Figure 6-5.

Figure 6-5.A sprite sheet consisting of images for an animation of an explosion



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.