Let’s explore the situation we just saw. Download this world, at the bottom of this page, which has the zombie and the pier set up already (warning: it’s a different size than the one shown in the video, so you will have to figure out the right length for yourself!).
Download the world at the bottom of the screen: Mod7.2-ZombiePierStarterWorld.a2w
We’ve lined the zombie up at the left side of the pier and he’s facing directly down the pier. Make the zombie move forward some number of meters. Just take a guess to start. Maybe 5 meters.
Hit Play. How did it go? How would you describe what the zombie did?
Experiment several times changing the parameter controlling how far to move forward, until you get it so that it looks like the zombie is walking exactly to the end of the pier (you can decide if that means his toes are still on the pier, or midfoot, or heels – whatever you want).
Now that you’ve got that, let’s change the size of the pier (don’t worry, we won’t make you go through that trial and error process again!). You can choose, either make your pier longer or shorter.
Now, predict in your head – what do you think will happen when you hit play? Will the zombie go too far? Will it not go far enough? Imagine what will happen.
Then hit play to see if your prediction was right.
OK. Now we’ll learn a smarter and more flexible way to write our program so that the zombie will always walk to the end of the pier, no matter the size of the pier.
What we need to be able to do is have the number that is the parameter to the move method be able to change, based on the size of the pier. But the only way we've known to control the amount something moves is with a constant value – a specific number that we type in when we write the code. Now, we are going to replace the constant value with a calculated value that is available as a function of the size of the pier object.
Click on the pier object. Now, in the lower left window where you see the methods for the pier object, look at the top of that window and select the function tab (currently the method tab is selected). Once you have done that you see a different set of tiles in the lower left window – all the tiles are purple, not yellow. Each of these tiles is a function - each calculates a value. (Note: A value can be a number or something called a Boolean value that is true or false -- but don't worry about Booleans yet -- we'll get to those soon!)
The first set of functions calculate things about an object’s proximity (closeness) to things. We’ll come back to these soon. Scroll down to the section of functions labeled size. The first three in this list we will use a lot as they tell us the size of an object. Because our object is three-dimensional, it has a width function, a height function, and a depth function to give us those three measurements. The only odd thing is that, as we've mentioned before, not all objects are “designed” the same way, given the way we have set up the pier, what looks like the width to us (length from left to right), is actually the depth.
In Alice, does the Blue line (of the Object Axes) represent depth, the red width, and the green height for all objects? - dp Jan 2016
So, we want to have the zombie move forward not a fixed number of meters, but a number of meters that is exactly the depth of whatever the pier object is (based on how it got set up in the world). We want to replace the parameter in the forward method with a function call to the pier’s depth function. Drag the pier depth function tile over the static (constant) number of meters.
Press Play to see if it worked. <Note: Since our world starts with the zombie’s heels on the far left of the pier, he’s going to move so that just his heels are on the far right of the pier. That might look a little different than the way you did it originally.>
Communication Check:
We're going to play around with our code some more -- but before we do, let's make sure we know how we describe in English what this code is doing. We say that when we execute the zombie move method, the parameter indicating how far forward to move is calculated by our function, pier's depth. We say that the value is returned from the pier's depth function. We execute a function when we encounter it in a statement (for now, as a parameter). The move method then is executed as if the number returned by the function was actually typed in the howFar parameter.
OK. But how do you know it will work in other situations? How do you know that you can trust the function to return the “right” value?
[Q7.2.1]: How would you test your world to make sure that your use of the function is giving you “the right” results in more than this one case? Describe one other test you would perform to make sure your zombie always walked the whole length of the pier. What would you change? What would you observe?
Let’s play around a little bit more to make sure we understand these size functions.
[Q7.2.2]: What do you think would happen if, for this example, we changed the parameter to the move method to be the result returned from the width function?
Try that out to see if you are right. Then, for fun, try out having the zombie move forward the “height” of the pier’s distance.
[Q7.2.3]: Suppose you change the move method to move down the distance that is the height of the pier? How would you describe what would happen?
Don't forget: Download the file linked below. Launch Alice on your computer, then use File -> Open World and select the file you downloaded.
Mod7.2-ZombiePierStarterWorld.a2w
Save the world as LastNameFirstName-7.2-ZombiePier.a2w