events (2) (alice in action, ch 6) slides credit: joel adams, alice in action cs 120 lecture 10 28...

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

Upload: clementine-easter-atkinson

Post on 31-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Events (2)(Alice In Action, Ch 6)

Slides Credit: Joel Adams, Alice in Action

CS 120 Lecture 10

28 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

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

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

Alice in Action with Java 5

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

Alice in Action with Java 6

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

Alice in Action with Java 7

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

Alice in Action with Java 8

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

Alice in Action with Java 9

Helicopter Flight Simulation

Alice in Action with Java 10

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

Alice in Action with Java 11

Helicopter Flight Simulation

Alice in Action with Java 12

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

Alice in Action with Java 13

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

– Depressing arrow key sends a Left or Right argument

Alice in Action with Java 14

Helicopter Flight Simulation

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

Alice in Action with Java 15

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

Alice in Action with Java 16

Alice Tip: Using 3-D Text (continued)

Alice in Action with Java 17

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

Alice in Action with Java 18

Repositioning Text that is Off-Camera (continued)

Alice in Action with Java 19

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

Alice in Action with Java 20

Adding a Background (continued)

Alice in Action with Java 21

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

Alice in Action with Java 22

Making Text Appear or Disappear (continued)

Alice in Action with Java 23

Making Text Appear or Disappear (continued)

Alice in Action with Java 24

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 Due Monday @ Lab Session• Midterm: Friday, Oct 12th on material through today• We will start Java next week.

– Make sure you have the Java textbook.

• Alice Project – Details on Monday in Lab– Can not be dropped– Groups of 1 to 3 people– In-Class Demos on Tuesday Oct 15th

25