Declare an instance of the HUD
and Renderer
classes as members of GameEngine
as highlighted in this next code.
class GameEngine extends SurfaceView implements Runnable, GameStarter { private Thread mThread = null; private long mFPS; private GameState mGameState; private SoundEngine mSoundEngine; HUD mHUD; Renderer mRenderer;
Initialize the instances of the HUD
and Renderer
classes in the GameEngine
constructor as highlighted next.
public GameEngine(Context context, Point size) { super(context); mGameState = new GameState(this, context); mSoundEngine = new SoundEngine(context); mHUD = new HUD(size); mRenderer = new Renderer(this); }
Now we can add a call to the draw
method of the Renderer
class in the run
method as highlighted next.
@Override
public void run() {
while (mGameState.getThreadRunning()) {
long frameStartTime = System.currentTimeMillis();
if (!mGameState.getPaused()) {
// Update all the game objects here
// in a new way
}
// Draw all the game objects here
// in a new way
mRenderer.draw(mGameState, mHUD);
// Measure the frames per second in the usual way
long timeThisFrame = System.currentTimeMillis()
- frameStartTime;
if (timeThisFrame >= 1) {
final int MILLIS_IN_SECOND = 1000;
mFPS = MILLIS_IN_SECOND / timeThisFrame;
}
}
}
18.189.31.26