Game Development Reference
In-Depth Information
void updatePhysics()
fires once on every step of the loop. Use this method to
update the sprites on the game.
•
boolean gameOver()
can be used to check if the user has terminated the game and
perhaps get a final score. Its implementation is mostly optional.
•
long getScore()
can be used to get the user's final score.
•
Listing 4-5.
The Game Loop Using Timer Tasks
public abstract class ArcadeGame extends LinearLayout
{
// Update timer used to invalidate the view
private Timer mUpdateTimer;
// Timer period
private long mPeriod = 1000;
// ....
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
try {
// Init game
initialize();
/**
* start update task. Which will fire onDraw in the future
*/
startUpdateTimer();
} catch (Exception e) {
e.printStackTrace();
}
}
// ...
/**
* A timer is used to move the sprite around
*/
protected void startUpdateTimer() {
mUpdateTimer = new Timer();
mUpdateTimer.schedule(new UpdateTask(), 0, mPeriod);
}
private class UpdateTask extends TimerTask {
@Override
public void run() {
updatePhysics();
/**
* Cause an invalidate to happen on a subsequent cycle through