+ All Categories
Home > Documents > Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user...

Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user...

Date post: 02-Jan-2016
Category:
Upload: alfred-arnold
View: 219 times
Download: 0 times
Share this document with a friend
Popular Tags:
19
Parameters and Event-Handler Methods Alice
Transcript
Page 1: Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.

Parameters and Event-Handler Methods

Alice

Page 2: Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.

Mouse clicks

Interactive programs often allow the user to mouse click an object in the display.

buttons in a windows-based interface targets in a game checklist of items on a form

In this session, we look at how to pass information about a mouse clicked object to an event handler method.

Page 3: Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.

Example

People are trapped in a burning building and the user will select which person will be rescued next.

Page 4: Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.

Storyboard

Three people are to be rescued. So, we could write three different methods.

Page 5: Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.

A Better Solution

A better solution is to write one event handler method and send in the information needed to perform the action.

firetruck.savePerson:

parameters: whichFloor, whichPerson, howFarDo in order point ladder at whichFloor extend the ladder howFar meters whichPerson slide down the ladder to the fire truck pull the ladder back howFar meters

whichFloor and whichPerson are Object parameters

howFar is a Number parameter

Page 6: Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.

Demo

Demonstration of the code for firetruck.savePerson

Page 7: Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.

Three events The argument sent to the parameters depends on which person is mouse clicked.

NOTE: in setting up our demo world, we positioned the fire truck so the distance of the ladder from the 1st floor is 1 meter, 2nd floor is 2 meters, and 3rd floor is 3 meters

Page 8: Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.

Example 2

Zeus was a powerful god in Greek mythology. When Zeus was angry, he would shoot a thunderbolt out of the heavens to strike anyone who got in the way.

The user will choose the philosopher who will be the next target of Zeus’s anger.

Page 9: Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.

Storyboard

A possible design is a method with an Object parameter, named who, for the object that was clicked.

The actions in this storyboard are complex.

We can break the actions down into simpler steps using stepwise refinement.

Event: An object is mouse-clicked

Event handler: shootBolt

Parameter: who — the object that was clicked

Do in orderprepare to strike the object that was clickedthunder plays and lightning strikes the object that was clickedlightning is repositioned for the next strike

Page 10: Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.

Event: An object is mouse-clicked

Event handler: shootBolt

Parameter: who — the object that was clicked

Do in ordercall prepareToShoot method — send who as the targetcall lightningAndThunder method — send who as the targetlightning move to cloud’s position

prepareToShoot:

Parameter: targetDo together turn Zeus to face the target make the lightning bolt visible

lightningAndThunder:

Parameter: target Do together play sound call specialEffects method — send target

specialEffects:

Parameter: target

Do in order Do together lightning bolt move to target smoke move to target Do together set smoke to visible set lightning to invisible call smoke cycle — built-in method set target color to black move target up and down

Page 11: Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.

A driverThe shootBolt method is at the top level of our design.

It calls other methods (prepareToShoot and lightningAndThunder) and controls the overall action of the program – we call this a driver.

Page 12: Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.

One link

In the fire rescue example, we used three links – one for each person in the burning building.

In this example, we use only one link by selecting “object under mouse cursor” as the argument.

Page 13: Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.

prepareToShootIn setting up the initial scene, we made the lightning bolt invisible by setting its opacity to 0 (0%). (Review Tips & Techniques 4 for more details about opacity.)

To prepare to shoot the lightning bolt, it must be made visible – set the opacity back to 1 (100%).

Page 14: Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.

lightningAndThunderCoordinate the sound of thunder with lightning and other special effects.

Page 15: Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.

specialEffectsThe smoke.cycleSmoke is a built-in instruction with a duration of about 2 1/2 seconds.

Page 16: Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.

move to

Several statements in the shootBolt and specialEffects methods use a move to instruction

The move to instruction moves an object to a particular position in the world. In the example above, the lightening bolt is moved to the position of the cloud.

(The move to instruction is described in detail in Tips & Techniques 2.)

Page 17: Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.

move to with an object parameterIf an object parameter is used to specify a target position, the process of creating a move to statement involves two steps:

1) Drag in an arbitrary object as the parameter

2) Substitute the object parameter name

This subterfuge is necessary because an Object parameter is

only a placeholder for an object – not an actual object.

Page 18: Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.

DemoTest run of Zeus world.When parameters are used in interactive programming, it is especially important to test that all possible parameter values work as expected.

What happens if you click on each philosopher, one at a time?

Also try things that shouldn’t work.What happens if you click on a column?What happens if you click on a philosopher twice?

Page 19: Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.

Assignment

Read Chapter 5-2, Parameters with Interactive Programming

Read Tips & Techniques 5

Lab 5-2


Recommended