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

events
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

1

Events

(Alice In Action, Ch 6)

Slides Credit: Joel Adams, Alice in Action

CS 120 Lecture 09

25 September 2012

Alice in Action with Java 2

Objectives

  • Create new events in Alice
  • Create handler methods for Alice events
  • Use events to build interactive stories
slide-2
SLIDE 2

2

Alice in Action with Java 3

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 4

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

slide-3
SLIDE 3

3

Alice in Action with Java 5

Events (continued)

Alice in Action with Java 6

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

slide-4
SLIDE 4

4

Alice in Action with Java 7

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 8

The Right Door (continued)

slide-5
SLIDE 5

5

Alice in Action with Java 9

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 10

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

slide-6
SLIDE 6

6

Alice in Action with Java 11

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 12

The Right Door Revisited (continued)

slide-7
SLIDE 7

7

Alice in Action with Java 13

Event Handling is Simultaneous

  • Example: left door tells jokes while right door turns
  • Alice handles simultaneous events well

Alice in Action with Java 14

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

slide-8
SLIDE 8

8

Alice in Action with Java 15

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

Student To Do’s

  • Readings:

– Alice in Action, Chapter 6

  • Homework posted on Web

– Demo next Monday in Lab 16