Now you can move on to create a system that enables you to accurately set how quickly your game runs. No longer will the game loop be subject to the whims of the device. To do this, you use a timer and then adjust the movement based on how much time has elapsed. This means that if one cycle takes a very long time and another takes a short amount of time, the sprite will move a certain distance depending on that value. Looking at the code is the best way to understand this type of method. Follow these steps:
- Replace the code in the
synchronized
block inGameLogic.java
with the code in Listing 2-13.try {
Thread.sleep(30);
}
catch (InterruptedException e1) {
}
long time_interim = System.currentTimeMillis();
int adj_mov = (int)(time_interim - time_orig);
mGameView.update(adj_mov);
time_orig = time_interim;
this.mGameView.onDraw(canvas);- At first, this entire snippet looks foreign. In reality, it performs a couple of simple tasks:
- The
try
-catch
block tells the tablet to wait for 30 milliseconds before continuing. This operation can produce an exception that you don’t deal with.- Previously, in the
run
function right next to the declaration of theCanvas
object, you made two long variables namedtime_orig
andtime_interim
.Time_orig
was set to the current system time withlong time_orig = System.currentTimeMillis();
. Now you settime_interim
to the time and determine how much time has elapsed. You store this in the integeradj_mov
, which stands for adjusted movement. TheUpdate
function in theGameView
class is changed to accept this integer as an argument. When this has been completed, the original time is set to the current time, and the view is refreshed by calling theonDraw
method.- Add the code from Listing 2-14 to the
update
method inGameView
.public void update(int adj_mov) {
sprite.update(adj_mov);
}- Listing 2-15 shows that the
adj_mov
variable is passed along to the sprite in order for it to be incorporated into the movement.- In this case, the sprite
update
method multipliesx_move
andy_move
by the change in time. I changed the speed constants to1
in order to keep the movement at a reasonable pace. This makes sense because if the computations take a long time, then the movement is multiplied by a greater number. If the processing is quick, the sprite doesn’t move nearly as far. The idea of controlling a game’s frames per second has a variety of implications that you take advantage of in later projects.
Although you would imagine most games wanting to have a time element, many apps can get away without worrying about this. Think about a chess or tic-tac-toe game. In a turn-based game, timing isn’t an important aspect.
Note Sample programs are available from Android’s reference guide that you can look to for inspiration on different types of games. Check out this page for the code sources: http://developer.android.com/resources/browser.html?tag=sample
. Be wary of the fact that the majority of the programs were written for earlier versions of Android, such as 2.2 or 2.3. You may want to create an emulator specific to that version if you’re truly interested in the examples. Porting them to Android 3.0 isn’t difficult; you can probably do so by enlarging the graphics and screen size.
3.141.2.133