+ All Categories
Home > Documents > Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events...

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

Date post: 30-Dec-2015
Category:
Upload: agatha-lucas
View: 221 times
Download: 0 times
Share this document with a friend
Popular Tags:
56
Alice in Action with Java Chapter 6 Events
Transcript
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

• Interactive program: processes data input by a user

• Event: action generated by a user or a program – Ex: When the world starts caused by Play click

• Two steps to making a program respond to an event– Choose (or define) a method to handle the event– Tell Alice to invoke the method when the event occurs

• Event handler: method called in response to an event

• Event-driven program: based on events and handlers

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

• First step: stop door from automatically telling jokes

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

Handling Mouse Clicks: The Magical Doors (continued)

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

• How to handle a mouse event– Choose or define a method

• Define a method when responsive behavior is complex

– Create an event that invokes the handler

• Enabling the right door to respond to a mouse event– Add When the mouse is clicked on something– Specify castle1.door as the event source– Use a turn()message to handle the event

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 Right Door (continued)

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 (continued)

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 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 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 Left Door (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

The Right Door Revisited

• Two-state behavior– Describes one of two states that an object can occupy– The state is determined by the value of a property– An if statement controls switching between states

• Logic error: right door continues to turn with user click

• Right door should open if closed, and close if open

• 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 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

The Right Door Revisited (continued)

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

The Right Door Revisited (continued)

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

The Right Door Revisited (continued)

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

Event Handling is Simultaneous

• Example: left door tells jokes while right door turns

• Alice handles simultaneous events well

• 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

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

Event Handling is Simultaneous (continued)

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

Categorizing Events

• Mouse event: triggered by mouse movement or click

• Keyboard event: triggered when user presses a key

• Program event: triggered when variable value changes

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

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 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

Design

• Six keys for six types of 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 position and mnemonic values

• Choosing correct keys improves usability

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

Programming in Alice

• Setting up 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

• Making the helicopter’s propeller spin– Use heli blade()to handle the press of Play button

• Making the helicopter ascend– Define Boolean property inTheAir for helicopter – Define helicopter. ascend()

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

Programming in Alice (continued)

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

Programming in Alice (continued)

• 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

• 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 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

Programming in Alice (continued)

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

Programming in Alice (continued)

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

Programming in Alice (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

Programming in Alice (continued)

• Let arrow keys move <subject> event– Not appropriate for controlling helicopter movement– helicopter would move while on the ground

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

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

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

Programming in Alice (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

Programming in Alice (continued)

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

Programming in Alice (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

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 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

Alice Tip: Using 3-D Text (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

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 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 34

Repositioning Text that is Off-Camera (continued)

Page 35: 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 35

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 36: 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 36

Adding a Background (continued)

Page 37: 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 37

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 38: 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 38

Making Text Appear or Disappear (continued)

Page 39: 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 39

Making Text Appear or Disappear (continued)

Page 40: 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 40

Alice Tip: Scene Transitional Effects for the Camera

• Transition: bridge between two scenes (or shots)

• Some transition effects – Cut: instantaneous jump from one scene to another– Fade: darken existing scene, light another – Wipe: cover existing scene, uncover new scene

• Alice does not provide built-in transitional effects

• Alice does provide building blocks for transitions

Page 41: 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 41

Setup for Special Effects

• Creating shutters for your camera– Add four squares outside of the viewport – Change names to indicate shutter positions– Change each square color to black – Change each square.vehicle to camera– Move the shutters outside of the viewing area

• Shutters will be manipulated to create effects

Page 42: 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 42

Setup for Special Effects (continued)

Page 43: 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 43

Setup for Special Effects (continued)

Page 44: 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 44

The Fade Effect

• A fade effect darkens and then lightens a screen

• Create effect using complementary methods

• Overview of implementing fadeToBlack()– Set the topShutter’s opacity to zero percent– Move the topShutter to cover the camera viewport– Set topShutter opacity back to 100 percent

• Overview of implementing fadeFromBlack()– Reverse the actions of fadeToBlack()

• Use fadeToBlack()and fadeFromBlack()in pairs– The scene changes between method calls

Page 45: 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 45

The Fade Effect (continued)

Page 46: 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 46

The Fade Effect (continued)

Page 47: 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 47

The Barndoor Edge Wipe Effect

• Edge wipe: edges cover one scene, reveal another

• Barndoor wipe: shutters slide like doors of a barn– Vertical: doors close and open from sides of screen– Horizontal: doors close and open from top and bottom

• Create barndoor wipes with complementary methods

• Implementing verticalBarndoorClose()– Move the left and right shutters into the viewing area

• Implementing verticalBarndoorOpen()– Move the left and right shutters out of the viewing area

Page 48: 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 48

The Barndoor Edge Wipe Effect (continued)

Page 49: 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

The Barndoor Edge Wipe Effect (continued)

Page 50: 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 50

The Box Iris Wipe Effect

• Iris contains the open area of the screen

• End one scene by shrinking the iris

• As iris expands, the new scene is revealed

• Defining the box iris close effect– Simultaneously move all four shutters onto viewport– Sender specifies duration of the effect– Sender specifies percentage the iris is closed

• One parameter and two local variables are needed

• Box iris open effect is complementary to close effect

Page 51: 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 51

The Box Iris Wipe Effect (continued)

Page 52: 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 52

The Box Iris Wipe Effect (continued)

Page 53: 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 53

The Box Iris Wipe Effect (continued)

Page 54: 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 54

Reusing Transition Effects

• Limitations of save object as… technique– Cannot be used to save properties as objects– Consequence: camera cannot be saved with shutters

• Solution: define transitions in a template world

• Contents of template (named TransitionEffects) – camera, light, ground, and squares

• Using TransitionEffects template world– Open template as starting world for user story– Use File->Save world as… to save and rename

Page 55: 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 55

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

Page 56: 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 56

Summary (continued)

• Add 3D Text dialog box: tool for inserting text • square: convenient shape for creating a text

background and transition effects

• Transition effect: bridge between two scenes

• Types of transition effects: cut, fade, and wipe

• A transition effects template world includes ground, light, camera, and effects objects


Recommended