Presentation is loading. Please wait.

Presentation is loading. Please wait.

Learn about functions in Alice

Similar presentations


Presentation on theme: "Learn about functions in Alice"— Presentation transcript:

1 Learn about functions in Alice
What we will do today Learn about functions in Alice

2 Functionality A function asks a question and give an answer
Receives value(s) Performs computation on value(s) Returns (sends back) a value

3 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

4 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. 4

5 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.

6 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!

7 Design Storyboard 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? World.ballOverNet: Do in order toyball turn to face the net Do together toyball move up toyball move forward toyball move down

8 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?

9 Problem The ball does not bounce over the net
The problem – cannot tell “which way is up” from the perspective of the ball

10 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

11 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

12 Adjusting Style adjusting the style of motion makes the bounce more realistic

13 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.

14 Demo: A first attempt

15 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?

16 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!

17 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

18 Parameters/Arguments
Arguments is the Object/value passed to the function Parameter is the alias (for the argument) used by the function. 18

19 numberOfRevolutions function
distance is the parameter

20 Building a numberOfRevolutions function
Step 1 Step 2: Drag distance parameter to replace 1 in return

21 The numberOfRevolutions function
Step 3: Select math and divide. Select other and key in 3.14

22 The numberOfRevolutions function
Step 4: Select the 3.14. Pick math and 3.14 * Select 1

23 The numberOfRevolutions function
Step 5: Replace the 1 with the width of the ball from the functions

24 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?

25 Creating a Roll Method Create a class level method for realistic roll
problem: amount of time to roll. (Demo) Elements of Visual Programming Functions

26 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

27 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

28 Classwork today

29 Labs Car Pyramid Make sure all tires rotate the appropriate amount
Have the mummy approach the pyramid Have the mummy scale the side the pyramid Have the mummy stop at the top


Download ppt "Learn about functions in Alice"

Similar presentations


Ads by Google