Game Development Reference
In-Depth Information
* the event loop. Use this to invalidate the View from
* a non-UI thread. onDraw will be called sometime
* in the future.
*/
postInvalidate();
}
}
/**
* Overridden these to process game events (life-cycle)
*/
// Update sprites here
abstract protected void updatePhysics();
// Init game
abstract protected void initialize();
// Game over
abstract protected boolean gameOver();
// Get score
abstract protected long getScore();
}
Let's take a closer look at the game life cycle of Asteroids.
Initializing the Game
Game initialization occurs by overriding the
initialize
method in the
Asteroids
class (see Listing 4-6).
It starts by loading sounds from the
res/raw
folder as follows:
Context ctx = getContext();
crashSound = new AudioClip(ctx, R.raw.a_crash);
clipTotal++;
explosionSound = new AudioClip(ctx, R.raw.a_explosion);
clipTotal++;
Here is where we use the
AudioClip
class to quickly load the raw resource using its ID. Note that we
also need the application
Context
. The game also keeps track of the total number of clips in the game.
Next, we create the star background using an array of
Point
s and the size of the screen:
void createStarts() {
int width = getWidth();
int height = getHeight();
// Generate the starry background.
numStars = width * height / 5000;
stars = new Point[numStars];