Game Development Reference
In-Depth Information
You should never reverse the order of code execution, as shown in
the preceding listing. For example, you could first try to render and
then update the game world. Now, in this case, the displayed game
world will always lag one frame behind of its actual state. The change
is very subtle and might even go unnoticed. This, of course, depends
on many factors. If it is an action game that requires fast reactions, it
will probably be much more noticeable as compared to a slow-paced
cardboard game with enough pauses to bridge the time gap until the
screen eventually shows the true game world state.
Next, add the following code to
resize()
:
@Override
public void resize (int width, int height) {
worldRenderer.resize
(width, height);
}
Whenever a resize event occurs, the
resize()
method of the
ApplicationListener
interface will be called. As this event is related to rendering, we want it to be handled
in
WorldRenderer
; therefore, simply hand over the incoming values to its own
resize()
method.
The same is almost true for the code to be added in
dispose()
:
@Override
public void dispose() {
worldRenderer.dispose();
}
Whenever a
dispose
event occurs, it is passed on to the renderer.
There is one more tiny addition to improve the code for execution on Android
devices. As you learned in
Chapter 2
,
Cross-platform Development - Build Once, Deploy
Anywhere
, there are system events on Android to pause and resume its applications.
In case of an incoming
pause
or
resume
event, we also want the game to either
stop or continue updating our game world accordingly. To make this work, we
need a new member variable called
paused
. Hence, add the following line of
code to the class:
private boolean paused;