Presentation is loading. Please wait.

Presentation is loading. Please wait.

Events (Alice In Action, Ch 6) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture 09 25 September 2012.

Similar presentations


Presentation on theme: "Events (Alice In Action, Ch 6) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture 09 25 September 2012."— Presentation transcript:

1 Events (Alice In Action, Ch 6) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture 09 25 September 2012

2 Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use events to build interactive stories

3 Alice in Action with Java3 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

4 Alice in Action with Java4 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

5 Alice in Action with Java5 Events (continued)

6 Alice in Action with Java6 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

7 Alice in Action with Java7 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

8 Alice in Action with Java8 The Right Door (continued)

9 Alice in Action with Java9 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

10 Alice in Action with Java10 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

11 Alice in Action with Java11 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

12 Alice in Action with Java12 The Right Door Revisited (continued)

13 Alice in Action with Java13 Event Handling is Simultaneous Example: left door tells jokes while right door turns Alice handles simultaneous events well

14 Alice in Action with Java14 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

15 Alice in Action with Java15 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

16 Student To Do’s Readings: –Alice in Action, Chapter 6 Homework posted on Web –Demo next Monday in Lab 16


Download ppt "Events (Alice In Action, Ch 6) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture 09 25 September 2012."

Similar presentations


Ads by Google