Debugging Harbor Defender

With a simple approach to how the game looks, you’re ready to try it. Load it as you would any game, and you see boats slowly appear and cruise down toward the castle. If you wait long enough, they pass through the castle and off the screen.

If things don’t work this way, or you get an error like the one displayed in Figure 8-8, or the game closes at startup, then you have some work to do. This section is dedicated to fixing common problems in game development for Android. It doesn’t go into specific problems because there is no way to anticipate every error. It should be fairly obvious how to fix errors in your code if Eclipse catches them, but the runtime issues can be more difficult.

Here is the process to use:

  1. Make sure you’re using LogCat to get information on the emulator. This is critical when you use Log.d in your program to alert you when certain events are triggered. LogCat also displays fairly detailed reports about errors.
  2. Don’t close the emulator when you get an error. Look at the issue in Figure 8-2. You may be tempted to close out immediately, but doing so erases the LogCat results. Instead, wait so you can diagnose the problem.
    images

    Figure 8-8. Runtime error in Harbor Defender

  3. Scroll up the LogCat readout, shown in Figure 8-9, and you should find phrases in a red font that signal where the error occurred. Luckily, the error notes the exact line numbers that were the problem.
    images

    Figure 8-9. LogCat NullPointerException

  4. You only need to pay attention to the earlier error lines in most cases. In this case, the onDraw() function failed when the cannons were being drawn. The reason is that I commented out the initialization of the cannon sprites. This is a common problem when you’re dealing with a game that has sprites being created and destroyed. Make sure all the sprites you reference for drawing or updating actually exist.
  5. A final suggestion for working with errors is to make your emulator smaller. If you have a relatively small screen size, then your emulator may take up most of the screen. This prevents you from looking at the LogCat while you’re working. To fix this, choose Run  Run_configuration. Then go to the Target tab and scroll down. In the command-line options, type in scale .8. This shrinks the emulator to 80% of its original size.

images Note If your best efforts to fix a problem don’t succeed, try searching for a solution on StackOverFlow (http://stackoverflow.com/). In the future, though, make small changes between tests. This way, you can go back to the previous state that worked. Be ready to always come back to something that you know worked.

The next chapter involves many different fixes and updates to the game. Most notably, you enable the user to move and rotate cannons. In your previous games, the player has never had this many options, and it will be a unique exercise.

Another addition is a point system that rewards the player for each boat that is destroyed. The physics must also be updated because you need to end the game once a boat strikes the castle rather than having the boat pass right through.

You also have to worry about a new factor: inappropriate user interaction. It makes sense for the user to click a piece of the pier to put a cannon onto it, but what if they miss the pier and click the ocean? This requires you to evaluate each input quickly and efficiently to respond immediately to the user, yet also prevent cannons from appearing where they don’t belong.

To finalize your work, you add input and logic to polish the overall appearance of the game.

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

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