Bringing It All Together

Now it’s time to tie all this information together with the previous chapter. You remember that the Game class was designed to contain all the relevant components of a game in a nice, orderly manner. In addition, the Actor class was created to allow for the creation of flexible game pieces that could be drawn during the game and respond to input from the player. The Actor class contains all the relevant information that a playable component in the game should know, including position and velocity. The ActorManager class allowed for dynamic class loading and contained a LinkedList of Actor classes used in the game. Now it’s time to take one of Actor class’s child classes and link it together in the Game class, as well as getting it drawn to the screen.

Actor Input Management

Before getting deeply into the reorganization of the Game class, let’s make a connection with the keyboard. In Chapter 2, the KeyListener interface was briefly discussed. Let’s make a class that will allow all of the input for an Actor to be contained in one easy-to-use class called PlayerAdapter.

import java.awt.event.*; class PlayerAdapter extends KeyAdapter {  private Player player;  public PlayerAdapter(Player p)  {    player = p;   }   public void keyPressed(KeyEvent e)    {     switch(e.getKeyCode())     {         case KeyEvent.VK_UP:         player.setY(player.getY()-5);         break;         case KeyEvent.VK_DOWN:         player.setY(player.getY()+5);         break;         case KeyEvent.VK_LEFT:         player.setX(player.getX()-5);         break;         case KeyEvent.VK_RIGHT:         player.setX(player.getX()+5);         break;         default:          break;         }   }   }

PlayerAdapter allows for easy modification and extension of a given Actor class’s movement. This adapter is registered with the object or objects that must take input. When something needs tweaking, it’s all stored here in one place.



Practical Java Game Programming
Practical Java Game Programming (Charles River Media Game Development)
ISBN: 1584503262
EAN: 2147483647
Year: 2003
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net