Game Development Reference
In-Depth Information
In the
MySuperAwesomeStartScreen.update()
method, we increase a
member called
x
by one each frame. This member holds the x coordinate
of the image we want to render. When the x coordinate value is greater
than 100, we reset it to 0.
4.
In the
MySuperAwesomeStartScreen.present()
method, we clear the
framebuffer with the color black (
0x00000000 = 0
) and render our
Pixmap
at position (
x
,0).
5.
6. The main loop thread will repeat steps 3 to 5 until the user quits the
game by pressing the back button on their device. The
Game
instance will
call then call the
MySuperAwesomeStartScreen.dispose()
method, which
will dispose of the
Pixmap
.
And that's our first (not so) exciting game! All a user will see is that an image is moving from
left to right on the screen. It's not exactly a pleasant user experience, but we'll work on that
later. Note that, on Android, the game can be paused and resumed at any point in time. Our
MyAwesomeGame
implementation will then call the
MySuperAwesomeStartScreen.pause()
and
MySuperAwesomeStartScreen.resume()
methods. The main loop thread will be paused for as long
as the application itself is paused.
There's one last problem we have to talk about: frame rate-independent movement.
Frame Rate-Independent Movement
Let's assume that the user's device can run our game from the previous section at 60FPS. Our
Pixmap
will advance 100 pixels in 100 frames as we increment the
MySuperAwesomeStartScreen.x
member by 1 pixel each frame. At a frame rate of 60FPS, it will take roughly 1.66 seconds to
reach position (100,0).
Now let's assume that a second user plays our game on a different device. That device is
capable of running our game at 30FPS. Each second, our
Pixmap
advances by 30 pixels, so it
takes 3.33 seconds to reach position (100,0).
This is bad. It may not have an impact on the user experience that our simple game generates,
but replace the
Pixmap
with Super Mario and think about what it would mean to move him in a
frame-dependent manner. Say we hold down the right D-pad button so that Mario runs to the
right. In each frame, we advance him by 1 pixel, as we do in case of our
Pixmap
. On a device that
can run the game at 60 FPS, Mario would run twice as fast as on a device that runs the game
at 30 FPS! This would totally change the user experience, depending on the performance of the
device. We need to fix this.
The solution to this problem is called frame-rate-independent movement. Instead of moving our
Pixmap
(or Mario) by a fixed amount each frame, we specify the movement speed in units per
second. Say we want our
Pixmap
to advance 50 pixels per second. In addition to the 50-pixels-
per-second value, we also need information on how much time has passed since we last moved
the
Pixmap
. This is where this strange delta time comes into play. It tells us exactly how much