====== More On Methods, Properties ====== ---- * We will complete the Alice program that you started in the [[alice_class_level_and_world_level_methods|Class Level and World Level Methods]] tutorial. ===== Loading the World ===== * For this tutorial, you can use your completed version (//methodStart.a2w//) from [[alice_class_level_and_world_level_methods|Class Level and World Level Methods]] tutorial. Or you can download a [[http://rockhopper.monmouth.edu/~jchung/cs175/fa11/alice/lect/methodStart1b.a2w|completed version here]]. ===== Part 1: Method Parameters ===== * Now that the kangaroo and the turtle have raced, let's make a method for the kangaroo to hop back to the turtle and challenge him to a race again. * Click on the kangaroo, create new method, and name it 'challenge'. * Drag a 'do in order' into your method. * Then, click on kangaroo in the object area, drag it into the method and select the method 'turn to face' - select the turtle - the entire turtle. {{ http://rockhopper.monmouth.edu/~jchung/cs175/fa11/alice/lect/writeChallenge_16.png }} * When you finish dragging all of the instructions into your method, it should look like this: {{ http://rockhopper.monmouth.edu/~jchung/cs175/fa11/alice/lect/writeChallenge_17.png }} * What would happen if our kangaroo was put into a world where there is no turtle and attempted to use the //kangaroo.challenge// method that we just wrote? * Alice would throw up an error message (best case) or crash (worst case). * **A class-level method should not have any direct references to other objects or world-level methods.** * In other words, a class-level method, to be really useful, should be independent of any particular Alice world. * Instead of referring to the turtle in the first instruction of the //kangaroo.challenge// method, we're going to use a method **parameter**. * A parameter can be thought of as a place holder. * For example, in a different world, you may want your kangaroo to be able to challenge a turtle or a bunny or a penguin. * Inefficient: We could write three different methods: one for the kangaroo to challenge the turtle, another for the kangaroo to challenge the bunny and a separate one for the kangaroo to challenge the penguin. * Efficient: We could write one method that uses a method parameter that acts as a placeholder for any object that the kangaroo will challenge - such as a bunny, a penguin or a turtle. * To do this, click on //create new parameter// and name it //obj//. * Before you click okay, make sure you select the type //object//. {{ http://rockhopper.monmouth.edu/~jchung/cs175/fa11/alice/lect/Parameter_18.png }} * You should see that the //obj// parameter has appeared beside the name of the method. * Drag //obj// into the method to replace //turtle//. {{ http://rockhopper.monmouth.edu/~jchung/cs175/fa11/alice/lect/Parameter_19.png }} * To test your code, drag //kangaroo.challenge// into //world.myfirstmethod// underneath the //world.race// method that is already there. * When you drag //kangaroo.challenge// into the method, once you release your mouse you will have to select //turtle//, then entire turtle as the //obj// to replace parameter. {{ http://rockhopper.monmouth.edu/~jchung/cs175/fa11/alice/lect/resultingCode_25.png }} * Press //Play// to test your new method. ==== Testing kangaroo.challenge on another object ==== * To reinforce your understanding of parameters, let's apply the //kangaroo.challenge// method to another object. * Add the //tortoise// (from the Animal folder) to your world, somewhere down the road in front of the kangaroo. * Drag //kangaroo.challenge// into your //world.myfirstmethod// and select the tortoise as the parameter. * When you play your program, now after the race, the kangaroo challenges the turtle and then the tortoise. * Afterward, delete the tortoise and the challenge to the tortoise from the program. ===== Part 2: Properties ===== * We want to write a method to make the turtle go into his shell (//hide//). * Click on turtle in the object tree. * Click on the method tab and create a new method named //hide// (If you use the world given to you as a starter world, hide will have already been created, but there is no code in it). * We are going to make all of the turtle's body parts invisible at the same time, except for his shell. * To do this, first drag a //do together// into the //hide// method. * Then, click on the + beside turtle in the object tree. * Click on the 'backRightLeg'. * In the details area: Click on the properties tab. * Click on 'isShowing' and drag it into the //do together// block. * Set the value to false. * Click on the 'more' and set duration to 0.1 {{ http://rockhopper.monmouth.edu/~jchung/cs175/fa11/alice/lect/properties_20.png }} * Do the same thing for each of the body parts by clicking on each of these in the object tree- backLeftLeg, frontLeftLeg, frontRightLeg, tail and head - and dragging the isShowing property of each into the //turtle.hide// method. * Now test the //turtle.hide// method. {{ http://rockhopper.monmouth.edu/~jchung/cs175/fa11/alice/lect/theEnd_24.png }} ---- ===== Recap ===== * If you want to write a method in which an object interacts with another character, you can either write a world-level method or write a class-level method with parameters. * A class-level method with parameters is a good choice if you want to be able to **[[http://www.alice.org/index.php?page=faq#SaveObject|save your object to disk]]** so that it can perform your new method in different worlds. * Keep in mind that parameters are not only used in class-level methods. * For example, if you have five characters in your world and you want them to all flip together, you can write one world level method with an object parameter that flips; then in a do together, call the method for each of the objects in your world. * You can change certain properties of an object while you're writing a method. ----