Making and Moving the Figure


Processing User Input

The LocationBeh behavior class deals with user input via the keyboard.The figure can be moved forward, back, left, right, up, or down, and turned clockwise or counterclockwise around the y-axis via the arrow keys. The code in LocationBeh is similar to the code in the TourControls class in Tour3D (from Chapter 18) but simpler in many cases since there's no viewpoint manipulation.

The figure reference is passed in through the constructor:

     // globals     private Figure figure;     private WakeupCondition keyPress;     public LocationBeh(Figure fig)     { figure = fig;       keyPress = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);     }

If a key is pressed along with alt, then altMove( ) will be called; otherwise, standardMove( ) will be invoked. (An Alt-key pair is used to move the figure vertically up and down and slide it to the left and right.) Both move methods have a similar style:

     // global movement constants     private final static int FWD = 0;     private final static int BACK = 1;     private final static int LEFT = 2;     private final static int RIGHT = 3;     private final static int UP = 4;     private final static int DOWN = 5;     private final static int CLOCK = 0;   // clockwise turn     private final static int CCLOCK = 1;  // counter clockwise     private void standardMove(int keycode)     // moves figure forwards, backwards, rotates left or right     {       if(keycode == forwardKey)         figure.doMove(FWD);       else if(keycode == backKey)         figure.doMove(BACK);       else if(keycode == leftKey)         figure.doRotateY(CLOCK);    // clockwise       else if(keycode == rightKey)         figure.doRotateY(CCLOCK);   // counter-clockwise     } // end of standardMove( )     private void altMove(int keycode)     // moves figure up, down, slideleft or right     {       if(keycode == backKey )         figure.doMove(UP);       else if(keycode == forwardKey)         figure.doMove(DOWN);       else if(keycode == leftKey)         figure.doMove(LEFT);       else if(keycode == rightKey)         figure.doMove(RIGHT);       }  // end of altMove( )

The movement and rotation constants (FWD, BACK, etc.) are utilized as unique identifiers to control the move and rotation methods in Figure.


The keys are processed by calling doMove( ) and doRotateY( ) in the Figure object.



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

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