Download presentation
Presentation is loading. Please wait.
Published byMaría del Rosario Velázquez Rodríguez Modified over 6 years ago
1
Organizing common actions
Methods Organizing common actions Copyright © Curt Hill
2
A Problem The simple objects are just moved
Those which have moving parts should move them The fish should move its tail back and forth, open and close its mouth Doing it once is not problem More than once gets real tedious We do not want to use the move method but the swim method should take this into account Copyright © Curt Hill
3
The Solution A method is a group of instructions that are bundled together They can be created and then executed like any of the predefined methods that come with the actors The goal is now to create a swim method that moves the fish forward and wags the tail as it is moving Better yet it is easy to do Copyright © Curt Hill
4
Methods Three steps: Create a new method
Uses the button in Details Pane Put the new statements in the method Call the method from elsewhere Copyright © Curt Hill
5
Creation Next we look at the creation in three screens
In the first we have turtle selected The mouse is over the Create New Method Button Notice the hint showing Second we name the method In this case swim Third we now have two method tabs The leftmost is the first method which starts when the world starts The rightmost is the new empty method Copyright © Curt Hill
6
Copyright © Curt Hill
7
Copyright © Curt Hill
8
Copyright © Curt Hill
9
Put New Statements in Method
Same procedure as before Drag statements in In this case one move forward One set of turns for each foot A Do Together and four Do In Orders Copyright © Curt Hill
10
Copyright © Curt Hill
11
Calling the New Method User-defined methods are just like predefined methods Using a predefined method is just like using one you created Mostly Observe the following screen shot of the turtle swim Notice: New one is above the button It also has an edit button It is used in the Method Edit pane Copyright © Curt Hill
12
Copyright © Curt Hill
13
Differences Slightly different on the left
Somewhat different on the right Most methods have a parameter A parameter modifies how the action occurs The move method has two obvious parameters Direction Distance A move cannot be given without these Copyright © Curt Hill
14
Parameters A parameter is a value that is passed to the method when it is executed Some methods have no parameters Many methods have required parameters and optional parameters A required parameter must be given An optional parameter may be given otherwise there is a good default value Copyright © Curt Hill
15
Parameter for swim? Swim does a move but does not allow the move distance to be set Let us make the distance a parameter This will be plugged into the move directly How is this done? Observe the swim method Create New Parameter button Copyright © Curt Hill
16
Copyright © Curt Hill
17
Create New Parameter The Create New Parameter button brings up a dialog box In this box we choose a type and a name The name is how the parameter is referenced inside the method The type is the kind of data: Number, boolean, object, string Here is a shot of this: Copyright © Curt Hill
18
Copyright © Curt Hill
19
Using the Parameter Anywhere a constant number may be used a numeric parameter may now be used When a numeric is specified in a drop down list there are three options besides a constant Other – you specify on calculator Expression – a parameter or variable Math – a calculation using parameter Copyright © Curt Hill
20
Copyright © Curt Hill
21
Math Instead of a numeric we may also use a calculation
This gets somewhat tedious to specify but we can make it work To get an expression like: time / 4 we need to first specify the expression time then specify math Here is the second half Copyright © Curt Hill
22
Copyright © Curt Hill
23
Math Revisited The drag and drop approach gets to be a problem in math expressions that are somewhat complicated First step is set the item that is to left of operator Second step is to specify a math and then choose the operator and the item to right We will see this in next exercise Copyright © Curt Hill
24
One More Thought We typically use methods in two ways
Capture a common action so that we can use it again and again A swim or walk method Use it to organize our movie Each scene becomes a method The method is only called once, but it organizes our movie Copyright © Curt Hill
25
Method Exercise In this exercise use a method to make any type of motion more natural Call the method several times Use parameters Copyright © Curt Hill
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.