Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events...

34
Alice in Action with Java Chapter 6 Events

Transcript of Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events...

Page 1: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java

Chapter 6Events

Page 2: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 2

Objectives

• Create new events in Alice

• Create handler methods for Alice events

• Use events to build interactive stories

Page 3: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

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

Page 4: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 4

Events (continued)

Page 5: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 5

Handling Mouse Clicks: The Magical Doors• Goal: add events to world built in Section 5.2.3

– Review: 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

Page 6: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 6

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

Page 7: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 7

The Right Door (continued)

Page 8: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 8

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

Page 9: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

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

Page 10: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 10

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

Page 11: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 11

The Right Door Revisited (continued)

Page 12: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 12

Event Handling is Simultaneous• Example: left door tells jokes while right door turns

• Alice handles simultaneous events well

Page 13: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

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

Page 14: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 14

Handling Key Presses: A Helicopter Flight Simulator

• The problem– The mayor’s cat is lost and city government has halted– You need to use your helicopter to find the cat– Search begins at an airport outside the city

Page 15: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 15

Event Design

• Six keys for six types of helicopter movement– ‘a’ key: ascend– ‘d’ key: descend– Up arrow key: move forward – Down arrow key: move backward– Left arrow key: turn left– Right arrow key: turn right

• Keys are chosen for two reasons– Convenient positions and mnemonic values

• Choosing correct keys improves usability

Page 16: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 16

Helicopter Flight Simulation• Plan the world for a helicopter simulation

– Add airport, helicopter, city terrain, buildings, and a cat– Position the helicopter at the airport– Position camera to be peering out in front of helicopter– Set the camera’s vehicle property to be helicopter

Page 17: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 17

Helicopter Flight Simulation

• Event handling– Observation: feasible helicopter motion depends on its status in air• E.g. it can only descend, turn L/R, or move F/B, when it is in air

– Define Boolean property inTheAir for helicopter– In event handler methods, check the value of inTheAir and respond accordingly

Page 18: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 18

Helicopter Flight Simulation

• Making the helicopter ascend (continued)– Add a When a key is typed event– Change event to While a key is typed event– Associate the ‘A’ key with the event– Enable the handler to perform while ‘A’ key is pressed

Page 19: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 19

Helicopter Flight Simulation

Page 20: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 20

Helicopter Flight Simulation• Making the helicopter descend

– Define descend()method for helicopter• Method logic mirrors the logic of ascend()• Note that the ground provides a floor for the descent

– Enable descend()to perform while ‘D’ key is pressed

Page 21: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 21

Helicopter Flight Simulation

Page 22: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 22

Helicopter Flight Simulation• Define turnSlightly()to handle left or right turns

– helicopter turns only if inTheAir is true– turnSlightly()takes a Left or Right argument – Arrow keys are associated with the method – Depressing arrow key sends a Left or Right argument

Page 23: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 23

Helicopter Flight Simulation• Connect the left and right arrow keys with the method

– Depressing arrow key sends a Left or Right argument

Page 24: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 24

Helicopter Flight Simulation

• Define go()to handle forward or backward movement– Logic is similar to logic for turnSlightly()method

Page 25: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 25

Alice Tip: Using 3-D Text

• Helicopter simulator should have flight instructions

• Solution: add 3-D text that explains the interface

• Creating 3-D text for the simulator– Return to the Add Objects screen– Click Create 3D Text (at far end of Local Gallery)

– Add flight instructions in the text box– Click OK – Rename the text object, instructions

Page 26: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 26

Alice Tip: Using 3-D Text (continued)

Page 27: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 27

Repositioning Text that is Off-Camera

• Right-click instructions to access methods• Three settings for positioning text in front of camera

– Choose methods-> setPointOfView(<asSeenBy)->camera

– Choose methods->move(<direction>,<amount>) ->FORWARD->10 meters

– Choose methods->turn<direction>,<amount>) ->LEFT->1/2 revolution

• Linking text to camera and changing text color – Set instructions.vehicle to camera– Set instructions.color to yellow

Page 28: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 28

Repositioning Text that is Off-Camera (continued)

Page 29: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 29

Adding a Background

• Text for instructions is difficult to read

• Solution: provide a background for the text

• One way to add a background to instructions– Insert square object (Square is in Shapes Folder)– Resize and reposition square behind instructions– Set square.color property to black– Make light turn to face the instructions– Set the square.vehicle property to instructions

Page 30: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 30

Adding a Background (continued)

Page 31: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 31

Making Text Appear or Disappear

• Another example of two-state behavior

• Implementation strategy– Add handler to switch value of isShowing property

• Logic of toggleInstructionVisibility() – Negate the value of isShowing property– Apply negation to both square and instructions

• Completing the implementation of the handler– Add a When a key is typed event– Associate the toggle method with the spacebar

Page 32: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 32

Making Text Appear or Disappear (continued)

Page 33: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 33

Making Text Appear or Disappear (continued)

Page 34: Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.

Alice in Action with Java 49

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

• Add 3D Text dialog box: tool for inserting text