|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object group1.control.Controller
public class Controller
A class for a game controller.
TIP: When making the GUI, in one of the larger components you'll need to use:
addKeyListener(control);
setFocusable(true);
...for this class to work. That actually makes the GUI accept keyboard input, don'cha know.
This is written in a way to take better control of the keyboard events. It remembers whether certain keys are being pressed or have been hit.
Note that, although this class suggests purposes for each key, it it up to other classes to determine what each key will actually do.
Also note, it became necessary to determine whether the key is being held down or was hit. For instance, the character should only jump if the user hits "jump," not if "jump" is being held down.
--"is pressed" means that the key is being held down.
--"was hit" means that the key was newly pressed. This is only changed to true if the key was not being pressed before, and changes to false as soon as the key is released, or when another class asks this class if the key was hit.
Constructor Summary | |
---|---|
Controller()
Default constructor. |
Method Summary | |
---|---|
boolean |
isActionPressed()
Whether or not the action key is being pressed. |
boolean |
isDownPressed()
Whether or not the down key is being pressed. |
boolean |
isJumpPressed()
Whether or not the jump key is being pressed. |
boolean |
isLeftPressed()
Whether or not the left key is being pressed. |
boolean |
isRightPressed()
Whether or not the right key is being pressed. |
boolean |
isSelectPressed()
Whether or not the select key is being pressed. |
boolean |
isStartPressed()
Whether or not the start key is being pressed. |
boolean |
isUpPressed()
Whether or not the up key is being pressed. |
void |
keyPressed(java.awt.event.KeyEvent e)
keyPressed method to implement KeyListener. |
void |
keyReleased(java.awt.event.KeyEvent e)
keyReleased method to implement KeyListener. |
void |
keyTyped(java.awt.event.KeyEvent e)
keyTyped method to implement KeyListener. |
boolean |
wasActionHit()
Whether or not the action key was hit. |
boolean |
wasDownHit()
Whether or not the down key was hit. |
boolean |
wasJumpHit()
Whether or not the jump key was hit. |
boolean |
wasLeftHit()
Whether or not the left key was hit. |
boolean |
wasRightHit()
Whether or not the right key was hit. |
boolean |
wasSelectHit()
Whether or not the select key was hit. |
boolean |
wasStartHit()
Whether or not the start key was hit. |
boolean |
wasUpHit()
Whether or not the up key was hit. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Controller()
Method Detail |
---|
public boolean isJumpPressed()
public boolean isActionPressed()
public boolean isStartPressed()
public boolean isSelectPressed()
public boolean isUpPressed()
public boolean isDownPressed()
public boolean isLeftPressed()
public boolean isRightPressed()
public boolean wasJumpHit()
public boolean wasActionHit()
public boolean wasStartHit()
public boolean wasSelectHit()
public boolean wasUpHit()
public boolean wasDownHit()
public boolean wasRightHit()
public boolean wasLeftHit()
public void keyPressed(java.awt.event.KeyEvent e)
Note that keyPressed events are generated over and over. This is... bad for a game with frames and time clicks. Very bad. So, this method takes those events and simplifies them to more controlled boolean states.
keyPressed
in interface java.awt.event.KeyListener
e
- The KeyEvent caught.public void keyReleased(java.awt.event.KeyEvent e)
Note that keyReleased events are generated only once. We use this to our advantage to keep control over the states of the keys.
Note also, that this method resets "was hit" values to false. Otherwise, it might cause problems if we don't call for a button hit, then call for it later. If a player actually taps the button so fast that a time click can't pick it up, then I don't think they could be human, anyway...
keyReleased
in interface java.awt.event.KeyListener
e
- The KeyEvent caught.public void keyTyped(java.awt.event.KeyEvent e)
keyTyped
in interface java.awt.event.KeyListener
e
- The KeyEvent caught.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |