1 / 14

User Interactivity

User Interactivity. Chapter 3. AWT Event Model. AWT has own thread, “listening” to OS Listener: an object that receives events from another object Different types of listeners for different types of events. You didn’t hear about last night’s big event?

vonda
Download Presentation

User Interactivity

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. User Interactivity Chapter 3

  2. AWT Event Model • AWT has own thread, “listening” to OS • Listener: an object that receives events from another object • Different types of listeners for different types of events. You didn’t hear about last night’s big event? You must not be on the right notification list!

  3. How Listeners Work • User presses a key • OS sends the key event to the Java runtime thread • Java posts the event to AWT’s event queue • AWT dispatch thread posts the event to all objects that are KeyListeners • The KeyListener receives the event.

  4. Listeners are Interfaces • public class MyGame extends GameCore implements KeyListener{ • Interfaces vs Superclasses : • Use interfaces for behaviors, superclasses for ISA relationships • Interfaces typically give an object a behavior secondary to its main behavior

  5. Key Listeners • Used for firing, moving, not really for String input • Create an object that implements KeyListener: • keyPressed() • keyReleased() • keyTyped() (key held down) • Register the listener with the window you want to listen to Window window = screen.getFullScreenWindow(); window.addKeyListener(keyListener);

  6. Extras • Focus Traversal Keys – keys that already have meaning in a window interface (tab goes from one field to the next) window.setFocusTraversalKeysEnabled(false); • KeyCodes – how you know which key was pressed. int keycode = e.getKeyCode(); if(keycode == KeyEvent.VK_ENTER) • Consume the event so other Listeners don’t see it: e.consume(); • Beware small differences between OS’s

  7. Mouse Input • Three Types of Mouse Events: • Button clicks (MouseListener) • Mouse motion (MouseMotionListener) • Regular movement • Drag motion • Wheel scrolls (MouseWheelListener)

  8. Changing the Mouse • Sometimes you don’t want mouse motion to stop at the end of the screen. (First-person game) • Constantly reposition mouse in middle of screen. • Calculate distance moved from center each time to update display.

  9. Changing the Mouse • Your mouse icon can also look like: • Hand • Crosshairs • Hourglass • I (text based) • Make it go away altogether

  10. Threads • Threads – executions of code. • Multithreading – many threads running at once • Multiple threads accessing the same data can cause problems • Hit key “g” Hit key “->” - Find s’ position (p1) - Move s to p2 - Resize s - Redraw s at p2 - Redraw s at p1

  11. Synchronization • Draws need to happen one at a time. • Mouse movements need to happen one at a time. • “Lock” the code so that only one thread can execute the code at a time. publicsynchronizedvoid mouseMoved(MouseEvent e) {

  12. GameAction (420 engine) • Model a user interaction • Tell if a key or mouse button is pressed, and how many times • Each GameAction will map to some animation in the game: • Ex: Hitting “->” makes the screen scroll to the right.

  13. Input Manager (420 engine) • One class that will: • Handle all key & mouse events • Save the events • Map the events to game actions • Let the user configure the keyboard • It implements all listener types • Contains a keyCode to GameAction mapping

  14. Pausing • One GameAction can map to “pause” • What does it mean to pause a game? • Continue drawing • Stop updating • Stop taking input from user • In update of GameCore extension: if (!isPaused()){ checkGameInput(); player.update(elapsedTime);} • After coming back from being paused, clear the GameActions that happened. inputManager.resetAllGameActions();

More Related