events
play

Events (Alice In Action, Ch 6) 25 September 2012 Slides Credit: - PDF document

CS 120 Lecture 09 Events (Alice In Action, Ch 6) 25 September 2012 Slides Credit: Joel Adams, Alice in Action Objectives Create new events in Alice Create handler methods for Alice events Use events to build interactive stories


  1. CS 120 Lecture 09 Events (Alice In Action, Ch 6) 25 September 2012 Slides Credit: Joel Adams, Alice in Action Objectives • Create new events in Alice • Create handler methods for Alice events • Use events to build interactive stories Alice in Action with Java 2 1

  2. Events • Event: action generated by a user or a program – Ex: clicking the Play button will generate “ When the world starts ” event • Interactive programs, such as games, are event-driven • Two steps to making a program respond to an event – Choose or define a method to handle the event • Such a method is called an event handler • Define an event handler when responsive behavior is complex – Tell Alice to invoke the method when the event occurs Alice in Action with Java 3 More on Events • Conflicts can arise when coding parallel event logic – Example: two handlers modify the same property • How to avoid conflict – Ensure that handlers modify a property in sequence • Event categories – Mouse event: triggered by mouse movement or click – Keyboard event: triggered when user presses a key – Program event: triggered when variable value changes Alice in Action with Java 4 2

  3. Events (continued) Alice in Action with Java 5 Handling Mouse Clicks: The Magical Doors • Castle door tells random knock-knock jokes • Events that will be added to the original program – Right door opens when the user clicks it – Left door tells knock-knock joke when the user clicks it Alice in Action with Java 6 3

  4. The Right Door • First step: stop door from automatically telling jokes by making world.my first Method do nothing • Recall the two steps to handle an event – Use a predefined method or define a new method – Create an event that invokes the handler • Enabling the right door to respond to a mouse event – The door open/close motion feels simple  let us choose turn() to handle the event – Add When the mouse is clicked on something – Specify castle1.door as the event source Alice in Action with Java 7 The Right Door (continued) Alice in Action with Java 8 4

  5. The Left Door • Enabling the left door to respond to a mouse event – Specify castle.door2 as the source of this event – Drag-and-drop world ’s random joke method as handler • Test the program by clicking each door Alice in Action with Java 9 The Right Door Revisited • Logic error: right door continues to turn with user click • Right door should open if closed, and close if open – i.e. exhibiting the two-state behavior • General approach for dealing with multi-state behavior – Add an object property variable to store the object state – In object methods, use control flows such as the if statement to generate appropriate actions and perhaps also change the object state value afterwards Alice in Action with Java 10 5

  6. The Right Door Revisited • Fix: implement two-state behavior for right door – Add Boolean property rightDoorClosed to castle – Replacement handler: openOrCloseRightDoor() – Build turn logic around the value of rightDoorClosed Alice in Action with Java 11 The Right Door Revisited (continued) Alice in Action with Java 12 6

  7. Event Handling is Simultaneous • Example: left door tells jokes while right door turns • Alice handles simultaneous events well Alice in Action with Java 13 More on Events • Conflicts can arise when coding parallel event logic – Example: two handlers modify the same property • How to avoid conflict – Ensure that handlers modify a property in sequence • Event categories – Mouse event: triggered by mouse movement or click – Keyboard event: triggered when user presses a key – Program event: triggered when variable value changes Alice in Action with Java 14 7

  8. Summary • Event: action generated by a user or a program • User events: keyboard events and mouse events • Event handler: method called in response to an event • Event-driven program: program directed by events and handlers • Two-state behavior: pattern that switches the state of an object Alice in Action with Java 15 Student To Do’s • Readings: – Alice in Action, Chapter 6 • Homework posted on Web – Demo next Monday in Lab 16 8

Recommend


More recommend