Game Development Reference
In-Depth Information
Implementing the WorldController class
The following listing shows the first implementation of
WorldController
:
package com.packtpub.libgdx.canyonbunny.game;
public class WorldController {
private static final String TAG =
WorldController.class.getName();
public WorldController () { }
private void init () { }
public void update (float deltaTime) { }
}
This class has an internal
init()
method that initializes it. Naturally, all the
initialization code could also be put into the constructor. However, it appears to
be very helpful in many ways when an initialization code is available in a separate
method. Whenever we need to reset an object in the game, we do not always want
or have to completely rebuild it, thereby saving a lot of performance. Also, this
approach can greatly reduce the interruptions by the
Garbage Collector
(
GC
).
Instead, we try to actively reuse existing objects, which is always a recommended
design goal to maximize performance and minimize memory usage. This is
especially true for smartphones such as Android with limited resources.
The
update()
method will contain the game logic and will be called several hundred
times per second. It requires a delta time so that it can apply updates to the game
world according to the fraction of time that has passed since the last rendered frame.
The configurations of our starter classes use
vertical synchronization
(
vsync
) that is enabled by default. Using vsync will cap your frame
rate and likewise the calls to
update()
at a maximum of 60 frames
per second.