Implementing Timing and Complex Motion

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:

  1. Replace the code in the synchronized block in GameLogic.java with the code in Listing 2-13.

    Listing 2-13. Testing a Constant FPS Game

    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);        
  2. 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 the Canvas object, you made two long variables named time_orig and time_interim. Time_orig was set to the current system time with long time_orig = System.currentTimeMillis();. Now you set time_interim to the time and determine how much time has elapsed. You store this in the integer adj_mov, which stands for adjusted movement. The Update function in the GameView 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 the onDraw method.
  3. Add the code from Listing 2-14 to the update method in GameView.

    Listing 2-14. GameView.java with Revised update Function

    public void update(int adj_mov) {
            sprite.update(adj_mov);
            }
  4. 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.

    Listing 2-15. SpriteObject.java with Revised update Function

    public void update(int adj_mov) {
                    x += (adj_mov * x_move);
                    y += (adj_mov * y_move);
    }
  5. In this case, the sprite update method multiplies x_move and y_move by the change in time. I changed the speed constants to 1 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.

images 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.2.133