Organizing common actions Methods Organizing common actions Copyright © 2005-2017 Curt Hill
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 © 2005-2017 Curt Hill
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 © 2005-2017 Curt Hill
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 © 2005-2017 Curt Hill
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 © 2005-2017 Curt Hill
Copyright © 2005-2017 Curt Hill
Copyright © 2005-2017 Curt Hill
Copyright © 2005-2017 Curt Hill
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 © 2005-2017 Curt Hill
Copyright © 2005-2017 Curt Hill
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 © 2005-2017 Curt Hill
Copyright © 2005-2017 Curt Hill
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 © 2005-2017 Curt Hill
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 © 2005-2017 Curt Hill
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 © 2005-2017 Curt Hill
Copyright © 2005-2017 Curt Hill
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 © 2005-2017 Curt Hill
Copyright © 2005-2017 Curt Hill
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 © 2005-2017 Curt Hill
Copyright © 2005-2017 Curt Hill
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 © 2005-2017 Curt Hill
Copyright © 2005-2017 Curt Hill
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 © 2005-2017 Curt Hill
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 © 2005-2017 Curt Hill
Method Exercise In this exercise use a method to make any type of motion more natural Call the method several times Use parameters Copyright © 2005-2017 Curt Hill