CS320n – Elements of Visual Programming Sending Parameters to Event Handler Methods (Slides 5-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for.

Slides:



Advertisements
Similar presentations
CS320n –Visual Programming Class-level Methods and Inheritance – Part 1 Mike Scott (Slides 4-3-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger.
Advertisements

Class-level Methods Alice. World / Class Method World method A general method that may refer to multiple objects; not closely associated with any particular.
Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class.
Review of Chapter 4 Sections 1 and 2 World-level methods involve two or more objects break a large problem into smaller, logical units follow a design.
Class-level Methods and Inheritance MMP 220 Multimedia Programming This adapted material was prepared for students in MMP220 as as part of a curriculum.
Alice Inheritance and Event Handling. Inheritance Concept Consider this hierarchy; parents describe properties of children Animals Vertebrates MammalsFish.
Events Chapter 7. Interactivity The real world is interactive User determines order of actions instead of programmer.
Events Chapter 7. Interactive Real world is interactive User determines order of actions instead of programmer.
Parameters and Event-Handler Methods Alice. Mouse input Interactive programs often allow the user to use a mouse to click buttons in a windows-based interface.
Don Slater Wanda Dann
Parameters and Event-Handler Methods Sec 50 Web Design.
CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
While: Indefinite Loops Alice. Repetition In some situations, we don’t know exactly how many times a block of instructions should be repeated. All we.
CS320n –Visual Programming Random Numbers and Random Motion (Slides 6-3) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Fall 2007ACS-1805 Ron McFadyen1 Chapter 5 Interactive Programs.
CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
CS320n –Visual Programming Interactive Programs Mike Scott (Slides 5-1)
Copyright 2011 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Implementation Algorithm  Code World.my first method Control blocks Statements (methods,
Interaction: Events and Event Handling
Functions and Conditionals in Alice 1 Stephen Cooper Wanda Dann Barb Ericson September 2009.
CompSci 4 Chap 5 Sec 1 Oct 13, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
Study Guide For Test Chapter 5, 6,& 7 Test is Friday, May 15th.
Alice: Functions Alice Chapter 6 September 19, 2005.
Class-level Methods Chapter 6 part 1. Classes and Objects Classes o In Alice, classes are predefined as 3D models Objects o An object is an instance of.
Interaction: Events and Event Handling
Parameters and Event-Handler Methods Sec 50 Web Design.
Review For Test Chapter 4 & 5 Test is Wednesday, January 27th.
Event Driven Programming Chapter 5. Sequential Programming Computer-Centric Computer-Centric Program Runs as Programmer Intended Program Runs as Programmer.
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
CompSci 4 Chap 7 Sec 2 Apr 7, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
Events (2) (Alice In Action, Ch 6) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Today’s Agenda 1.Collect Pre-Lab 5 2.Collect Alice project storyboards 3.Events 4.Dummy Objects 5.Assign pair programming teams and meet upstairs for Lab.
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 MMP 220 Multimedia Programming This adapted material was prepared for students in MMP220 as as part of a curriculum.
CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
Methods Tutorial: Part Two By Deborah Nelson Duke University Professor Susan Rodger June 16, 2008.
What we will do today Learn about functions in Alice.
CS320n –Visual Programming Introduction to Recursion (Slides 8-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
CS320n –Visual Programming Advanced Recursion (Slides 8-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
CompSci 4 Chap 5 Sec 2 Oct 18, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Functions, Loops, and Parameters.
List Search Alice. Common Uses of Lists Iterating through a list of several like items to accomplish the same task with each item. As in the previous.
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions.
Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value.
Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person.
Skater World: Part Three By Deborah Nelson Duke University Under the direction of Professor Susan Rodger June 2009.
Interactive Programming Alice. Control of flow Control of flow -- how the sequence of actions in a program is controlled. What action happens first, what.
CS320n –Visual Programming Definite / Counted Loops (Slides 7-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
CompSci 4 Chap 5 Sec 2 Oct 9, 2008 Prof. Susan Rodger.
List Search Alice. Common Uses of Lists Iterating through a list of several like items to accomplish the same task with each item. As in the previous.
CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.
CompSci 4 Chap 5 Sec 1 and 2 Oct 14, 2010 Prof. Susan Rodger.
Using Placeholders to Simplify your Methods: Learning Methods, Part 2 By Deborah Nelson Duke University Professor Susan Rodger June 16, 2008.
CS320n –Visual Programming Problem Solving Case Study (Slides 6-3) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
CS320n –Visual Programming Execution Control with If / Else and Boolean Functions (Slides 6-2-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for.
Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person.
CS320n – Elements of Visual Programming List Search Mike Scott (Slides 9-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Parameters and Event-Handler Methods
Learn about functions in Alice
Learning Java with Alice 3.0 Game Design Kathy Bierscheid
CS320n –Visual Programming
Class-level Methods Alice.
CS320n –Visual Programming
Parameters and Event-Handler Methods
Exercise 1 Modify some parameters in the histogram.js and see what would happen (one by one) 20 in Line 2  in Line 3  in Line 15  .9.
Parameters and Event-Handler Methods
Parameters and Event-Handler Methods
Class-level Methods and Inheritance
Parameters and Event-Handler Methods
Presentation transcript:

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.

Visual ProgrammingSending Parameters to Event Handler Methods 2 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

Visual ProgrammingSending Parameters to Event Handler Methods 3 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.

Visual ProgrammingSending Parameters to Event Handler Methods 4 Example People are trapped in a burning building The user selects which person will be rescued next.

Visual ProgrammingSending Parameters to Event Handler Methods 5 Storyboard Three people are to be rescued. So, we could write three different methods. Event 1: click on guy1 Responding Method: Save guy on first floor Event 1: click on girl2 Responding Method: Save guy on second floor Event 1: click on girl3 Responding Method: Save guy on third floor

Visual ProgrammingSending Parameters to Event Handler Methods 6 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?

Visual ProgrammingSending Parameters to Event Handler Methods 7 Demo Demonstration of the code for firetruck.savePerson

Visual ProgrammingSending Parameters to Event Handler Methods 8 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)

Visual ProgrammingSending Parameters to Event Handler Methods 9 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.

Visual ProgrammingSending Parameters to Event Handler Methods 10 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

Visual ProgrammingSending Parameters to Event Handler Methods 11 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

Visual ProgrammingSending Parameters to Event Handler Methods 12 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

Visual ProgrammingSending Parameters to Event Handler Methods 13 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.

Visual ProgrammingSending Parameters to Event Handler Methods 14 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.

Visual ProgrammingSending Parameters to Event Handler Methods 15 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%).

Visual ProgrammingSending Parameters to Event Handler Methods 16 lightningAndThunder Coordinate the sound of thunder with lightning and other special effects.

Visual ProgrammingSending Parameters to Event Handler Methods 17 specialEffects The smoke.cycleSmoke is a built-in instruction with a duration of about 2 1/2 seconds.

Visual ProgrammingSending Parameters to Event Handler Methods 18 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.)

Visual ProgrammingSending Parameters to Event Handler Methods 19 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: 1)Drag in an arbitrary object as the parameter 2)Substitute the object parameter name This is necessary because an Object parameter is only a placeholder for an object – not an actual object.

Visual ProgrammingSending Parameters to Event Handler Methods 20 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?

Visual ProgrammingSending Parameters to Event Handler Methods 21 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.

Visual ProgrammingSending Parameters to Event Handler Methods 22 Penguin Slide

Visual ProgrammingSending Parameters to Event Handler Methods 23 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

Visual ProgrammingSending Parameters to Event Handler Methods 24 Hockey Game