240 likes | 376 Vues
CS320n – Elements of Visual Programming. Sending Parameters to Event Handler Methods (Slides 5-2). Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas. What We Will Do Today. Learn how to use parameters in methods that respond to events. (Event handler methods)
E N D
CS320n – Elements of Visual Programming Sending Parameters to Event Handler Methods (Slides 5-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
What We Will Do Today • Learn how to use parameters in methods that respond to events. (Event handler methods) • Do a programming example in the lab Sending Parameters to Event Handler Methods
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. Sending Parameters to Event Handler Methods
Example • People are trapped in a burning building • The user selects which person will be rescued next. Sending Parameters to Event Handler Methods
Storyboard • Three people are to be rescued. So, we could write three different methods. Event 1: click on girl2 Responding Method: Save guy on second floor Event 1: click on guy1 Responding Method: Save guy on first floor Event 1: click on girl3 Responding Method: Save guy on third floor Sending Parameters to Event Handler Methods
A Better Solution • Write one event handler method • send in the information needed to perform the action. firetruck.savePerson: parameters: whichFloor, whichPerson, howFar Do 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, whichPerson and howFar parameters What should their types be? Sending Parameters to Event Handler Methods
Demo • Demonstration of the code for firetruck.savePerson Sending Parameters to Event Handler Methods
Three events • The argument sent to the parameters depends on which person is mouse clicked. • Note: the fire truck is positioned so distance between floor X is X meters (distance to floor 3 is 3 meters) Sending Parameters to Event Handler Methods
Example 2 • Zeus is a powerful god in Greek mythology. • When Zeus is angry, he shoots a thunderbolt out of the heavens to strike anyone is bothering him • The user chooses the philosopher who will be the next target of Zeus’s anger. Sending Parameters to Event Handler Methods
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 – object clicked Do in order prepare to strike object that was clicked thunder plays and lightning strikes object clicked lighting is repositioned for next strike Sending Parameters to Event Handler Methods
Event: An object is mouse-clicked • Event handler: shootBolt • Parameter: who—the object that was clicked • Do in order • call prepareToShoot method — send who as the target • call lightningAndThunder method — send who as the target • lightning move to cloud’s position prepareToShoot: Parameter:target Do together turn Zeus to face the target make the lightning bolt visible lightningAndThunder: Parameter:target Do together play sound call specialEffects method — send target Sending Parameters to Event Handler Methods
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 Sending Parameters to Event Handler Methods
A driver • The 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. Sending Parameters to Event Handler Methods
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. Sending Parameters to Event Handler Methods
prepareToShoot • In 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%). Sending Parameters to Event Handler Methods
lightningAndThunder • Coordinate the sound of thunder with lightning and other special effects. Sending Parameters to Event Handler Methods
specialEffects • The smoke.cycleSmoke is a built-in instruction with a duration of about 2 1/2 seconds. Sending Parameters to Event Handler Methods
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, where is the lighting bolt moved to? (The move to instruction is described in detail in Tips & Techniques 2.) Sending Parameters to Event Handler Methods
move to with an object parameter • If an object parameter is used to specify a target position, the process of creating a move to statement involves two steps: • Drag in an arbitrary object as the parameter • Substitute the object parameter name This is necessary because an Object parameter is only a placeholder for an object – not an actual object. Sending Parameters to Event Handler Methods
Demo • Test 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? • What happens if you click on Zeus? Sending Parameters to Event Handler Methods
Classwork Monday in Lab • Create 2 worlds (or combine into 1 world) • Problem 14, page 140. Penguin slide: Create a world with a lake (environments) and at least 3 penguins on the slope. Make the program event driven. When a penguin is clicked it slides on its belly and spins as it slides. Each penguin should spin some number of times. When the penguin reaches the pond make it disappear into the pool. Write only one event handler method. Sending Parameters to Event Handler Methods
Penguin Slide Sending Parameters to Event Handler Methods
Exercise 2 • Problem 15, Page 141. • Create an interactive hockey game. Create a scene with a person on a lake. Set up a hockey net and give the person a hockey stick (both in the sports folder). Create 3 buttons (bumps from the shape folder) that determine how hard the player swings the stick and how fast the puck (another bump)travels towards the net Sending Parameters to Event Handler Methods
Hockey Game Sending Parameters to Event Handler Methods