290 likes | 356 Views
What we will do today. Learn about functions in Alice. Functionality. A function asks a question and give an answer Receives value(s) Performs computation on value(s) Returns (sends back) a value. Types of functions. The type of a function depends on the type of value it returns
E N D
What we will do today Learn about functions in Alice
Functionality • A function asks a question and give an answer • Receives value(s) • Performs computation on value(s) • Returns (sends back) a value
Types of functions • The type of a function depends on the type of value it returns • a calculated value (a number) • a specific object • a color • etc. 42
Built-in/user defined functions A built-in function is a previously existing function. You do not need to create Built-in functions. A User defined function is a function that you need to design and implement. You are adding functionality to the class when you add a user defined function. Distance between center points.
Built-in functions • Used one of Alice’s built-in functions • skateAround method for the cleverSkater • asked for the distance between the advanced skater and the object, target. (In the specific example shown, target was a penguin.) Distance between center points.
Example • How do we bounce a ball? Let’s bounce a ball over a net • Ball is 1 meter from the net to start • Ball should move up and forward, then down and forward • Note: Looks easy – but do not be deceived!
Design Storyboard World.ballOverNet: Do in order toyball turn to face the net Do together toyball move up toyball move forward Do together toyball move down toyball move forward • A possible storyboard • To reach the top of the net, • we know the ball should move forward 1 meter (we positioned the ball 1 meter in front of the net) • but how far upward should the ball move to clear the net?
Height • Can use the built-in height function to determine the height of the net and move the ball up that distance Demo – ballOverNetWork – what happens?
Problem • The ball does not bounce over the net • The problem – cannot tell “which way is up” from the perspective of the ball
Solution • We think “up” and “down” relative to the ground – so can orient the ball (and net) with the ground • Now, ball will bounce over the net
Realistic Motion • Turn the camera to look at the bounce from the side. • Is it realistic? • The default style for motion is “gently” • begin and end gently • can get more realistic by • making forward motion abrupt, • up motion end gently, and • down motion begin gently
Adjusting Style • adjusting the style of motion makes the bounce more realistic
Rolling the ball • How do we roll a ball along the ground? • Want a realistic motion rather than a slide • The ball must simultaneously move and roll.
Revising the Approach • The ball is made to roll 1 revolution. • What if we want the ball to roll a certain distance? • How can we make the ball roll the correct number of revolutions to cover a given distance along the ground?
Math to the rescue! • The number of revolutions depends on the size of the ball • The number of revolutions is distance / (Pi * diameter) • There is no built-in function to return the number of revolutions • Must write it!
Parameters • We want to return the value computed as Distance / Pi * diameter • Obviously, what is needed • The ball’s diameter • The ball object has a built-in width question • The distance the ball is to travel • Can be sent as a parameter to the question
Parameters/Arguments Arguments is the Object/value passed to the function Parameter is the alias (for the argument) used by the function.
numberOfRevolutions function • distance is the parameter
Building a numberOfRevolutions function Step 1 Step 2: Drag distanceparameter to replace 1 in return
The numberOfRevolutions function Step 3: Select math and divide. Select other and key in 3.14
The numberOfRevolutions function Step 4: Select the 3.14. Pick math and 3.14 * Select 1
The numberOfRevolutions function Step 5: Replace the 1 with the width of the ball from the functions
Demo: Calling the Function 10 is a test value. We should run the animation with several test values to be sure it works as expected. What happens if you use a negative value?
Functions Creating a Roll Method • Create a class level method for realistic roll • problem: amount of time to roll. (Demo)
Fixing Realistic roll • Set duration of to function of distance. • Try 2 meters per second • Fixes one problem, but what happens with negative numbers? • What is result of -5 / 2? • Fix, using a world level method that finds the absolute value of a number
Levels of functions • As with methods, you can write functions as either class-level or world-level. (what was the function we just wrote?) • Guidelines for class-level methods apply to class-level functions: • No references to other objects • No references to world-level functions • Built-in world-level functions are ok
Labs Car Make sure all tires rotate the appropriate amount Pyramid Have the mummy approach the pyramid Have the mummy scale the side the pyramid Have the mummy stop at the top