Forms & controls Material from Heim Chap 10 & 11 and … COMPSCI 345 / SOFTENG 350.
CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.
-
Upload
maximillian-williamson -
Category
Documents
-
view
225 -
download
0
description
Transcript of CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.
![Page 1: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/1.jpg)
CompSci 4Chap 4 Sec 3Sept 23, 2010
Prof. Susan Rodger
![Page 2: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/2.jpg)
Announcements
• Assignment 3 is due midnight today– Turn in zip file with 2 Alice worlds and
README text file OR upload all three files– Need a web page with 2 snapshots and
explanation for both Alice worlds• Assignment 4 out today• Skipping Chapter 5 (will come back to it)• Read Chapter 6.1 for next class• Reading Quiz
![Page 3: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/3.jpg)
Key concepts today
• World method vs. class method• Inheritance• Using built-in functions• Applying math for more precision
![Page 4: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/4.jpg)
Animated Actions
• Some actions are more naturally associated with a specific class of objects rather than the overall world
• Examples– A person walking– A wheel rolling– A fish swimming
![Page 5: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/5.jpg)
Class-level Methods
• Write a method to add abilities/functions to a specific class of objects– Class-level method– NOT world-level method
• Now show how to build class-level method
![Page 6: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/6.jpg)
An Example• How can we create a skate method for ice
skater objects?We need to:1) Associate the new
method with an ice skater
2) Write the new method to animate the ice skater
![Page 7: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/7.jpg)
The solution
To associate the animation with the ice skater– Select iceSkater tile
in Object Tree– Select methods tab
in details area– Click on “create
new method” button
![Page 8: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/8.jpg)
Storyboard for skate
• The slide actions– Require several motion instructions– We’ll break these two actions into smaller pieces– Technique is stepwise refinement
Skate:Do Together move skater forward 2 meters Do in order slide on left leg slide on right leg
![Page 9: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/9.jpg)
Refined storyboard for skate
Skate:Do Together move forward 2 meters Do in order slideLeft slideRight
slideLeft: Do in order lift right leg and turn upper body forward lower right leg and return body upright
slideRight:Do in order lift left leg and turn upper body forward lower left leg and return body upright
![Page 10: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/10.jpg)
Writing the code
• Next step – translate design into code• For slideLeft, possible translation is:
Design Step
Lift the right legTurn upper body forwardLower the right legReturn the body upright
Instruction
Turn the right leg forwardTurn upper body forwardTurn the right leg backwardTurn the upper body backward
![Page 11: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/11.jpg)
SlideLeft and Demo
Where is wait?
![Page 12: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/12.jpg)
Correspondence of design to codeSkate:Do Together move skater forward 2 meters Do in order slide on left leg slide on right leg
![Page 13: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/13.jpg)
Question
• Writing methods to make ice skater perform a skating motion – intricate task
• Would like to reuse these new methods in another world
• How can you make skate method available for an ice skater in a different world?
![Page 14: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/14.jpg)
Answer: Save out as a new class
1) Rename iceSkater as cleverSkater
2) Save out as a new class. Alice saves the new class as CleverSkater.a2c
![Page 15: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/15.jpg)
Inheritance• The CleverSkater class
– inherits all the properties and methods from the original IceSkater class
– has newly defined methods (skate, slideLeft, slideRight)
• In other programming languages, the concept of creating a new class based on a previously defined class is called inheritance
![Page 16: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/16.jpg)
Using CleverSkater• An instance of the CleverSkater class can be
added to a new world
![Page 17: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/17.jpg)
Benefits of Inheritence
• Inheritance supports– Reuse of program code
• Programmer writes code once• Use code later in different programs
– Sharing code with others on a team project
![Page 18: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/18.jpg)
Guidelines• To avoid misuse of class level methods
– Avoid references to other objects– Avoid calls to world-level methods– Play a sound only if the sound has been
imported and saved out as part of the new class• If these guidelines are not followed and an
instance of the new class is added to another world– Alice will open an Error dialog box to tell you
something is wrong
![Page 19: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/19.jpg)
Bad Example 1
![Page 20: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/20.jpg)
Bad Example 2
![Page 21: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/21.jpg)
Problem• What if you were convinced you needed to
write a class-level method where another object is involved?
• For example, a method for ice skater to skate around another object – here a penguin
![Page 22: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/22.jpg)
Solution• Class-level method with object parameter
cleverSkater.skateAround
Parameter: whichObject
Do in order Do together cleverSkater turn to face whichObject cleverSkater lift right leg cleverSkater move to whichObject cleverSkater turn around whichObject
![Page 23: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/23.jpg)
Translating Design into Code
• Most of skateAround storyboard easy to code
• Last two steps, require more thought– cleverSkater move to whichObject
• What distance should cleverSkater move?– cleverSkater turn around whichObject
• How do we tell cleverSkater to turn (in a circle) around another object?
![Page 24: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/24.jpg)
Built-in Functions (or questions)
• The built-in function distance to – used to determine the
distance the skater must move
![Page 25: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/25.jpg)
Calling the function• Code to move skater to whichObject
Oops, skater will collide with penguin!
Distance between two objects ismeasured center-to-center
![Page 26: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/26.jpg)
Expressions• To avoid collision
– Use math operator to create an expression that adjusts the distance
• Math operators in Alice addition + subtraction - multiplication * division /• Example:
![Page 27: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/27.jpg)
How to put in an Expression 2 3 1 4
Result:
![Page 28: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/28.jpg)
ResultStops before penguin Skates around penguin
![Page 29: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/29.jpg)
asSeenBy
• For skater to skate around another object– Pass whichObject as an argument to asSeenBy
parameter in turn instruction
![Page 30: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/30.jpg)
More on AsSeenBy• Use invisible object (isShowing set to false)
to have objects fly around in a circle
![Page 31: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/31.jpg)
Testing
• Each time you create a new class, test it!– Add an instance of new class to new world– Write a short test program
• Test each new method
• Testing increases your confidence in the ability to reuse your code in other worlds
![Page 32: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/32.jpg)
Classwork todaycreate a new class example
crouching pouncing
walking turn and smile
![Page 33: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/33.jpg)
Scene Change Highlights
• Import a class with all the ground color textures
• Fade to black, fade back in• Must change the atmosphereColor and
ambientLightColor to black and then change back
• Need to STORE THE OLD color values – Create a variable for storing the color
![Page 34: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/34.jpg)
The color property variables
![Page 35: CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051105/5a4d1b7f7f8b9ab0599ba52c/html5/thumbnails/35.jpg)
Classwork today• Create a new class
– Inherit from animal class that has 4 limbs– Create at least four new methods– One of the new methods should invoke one of the other
new methods• Create a second new class inherited from another
object– with at least 4 methods– At least two of the methods must have a parameter– Use AsSeenBy and isShowing each in some method (not
necessarily the same method)• Create a world to change scenes• Save out new classes and put in change scenes world• See handout for more details