In this section we are going to build up our somewhat complex cementTruck program, step by step as guided be the implementation strategy diagram we created on the last page. You might want to have a copy printed out or open in a separate internet tab. We've done the initial world design for you -- download cementTruckLoop.a2w from the bottom of the page (this is the same one from Part 1, so you can continue using that if you like).
Animation Specifics:
We should tell you that we did some animation "stuff" to make things go a bit easier. Specifically, whenever the truck moves, we want both the windupKey and the lightning objects to move along with the cementTruck -- so in the world setup, we called the method set vehicle to for all those objects and set their vehicle to the cementTruck. This means when the cementTruck moves all 4 of those objects (windupKey, lightning, lightning2, lightning3) will move with it.
We'll basically be working top to bottom through the diagram. At the top level, in World.my first method, we indicate we want to repeat something three times and then have a final "splash screen" of 3D Text.
Our implementation strategy diagram for World.my first method is this:
World.my first method
Loop 3 times
prepare
zoom
Game Over text becomes visible (opaque)
So we start by setting up our counted loop tile so that it will run 3 times. At the bottom of the window, with the other control tiles, drag out a loop tile. When you drop it, it will ask you how many times you want it to run. Since 3 isn't in the drop down list, choose other and type it in. (Hmmm... there's an option that says infinity! Infinity is a mathematical concept, but in Alice having a loop run infinity times means it will run the entire time your program plays.)
Now we want to add two more method calls to my first method. Both prepare and zoom are methods we are going to create ourselves. But the last line in the implementation strategy diagram we can handle without making a method call. In the object list, look for the 3D Text object, below, click on its properties (not its methods) and drag the opacity tile out into your program and select 1(100%) to make it opaque.
[Q10.3.1]: Where should you place the tile to set the opacity of the text -- on the loop tile or after the loop tile? Describe why you place it there, and what the code would do if you put it in the other place.
This is a very important thing to remember about loops. When people write code with loops, they often struggle to think -- does this instruction go on the loop tile? Or not? Here's the question to ask yourself: "Does this instruction need to happen one time? Or many times?" If the answer is many times, then it goes on the loop tile (we say that instruction goes "in the loop").
Next, we want to put the prepare and zoom method tiles on the loop 3 times tile. But they don't exist yet. So we will create them -- but we won't stop and fill in the code that goes in them yet. We want to finish off World.my first method first.
Be sure to click on the World object in the upper left hand window, then under methods, click on create new method. Do this two times to make the World.prepare and World.zoom methods. (Note: when you create a new method, it automatically opens up another program "tab" in the bottom right window. Just click on the World.my first method tab to get back to the code for that method.)
When you are done, drag each of those method tiles onto the loop.
[Q10.3.2]: What do you think will happen now? Guess in your head, hit Play and describe.
Why do you think the cementTruck doesn't move? Uh... right! We haven't written the code for the prepare method and the zoom method yet! But, now we can move on and do each one of those individually, without having to keep in our heads where they should be called. By abstracting all the actions that should happen in prepare, we made our code in World.my first method much easier to read and much simpler to think about. We can think -- "prepare" and "zoom" happens three times -- rather than cluttering our brain up with all the individual details in each step.
Abstraction is very important for humans. Our brains have physical limitations about how much we can keep "active" in our thoughts at any one time (Want to know more? Read on wikipedia about short-term memory). Computers don't have this problem. But because we can get computers to do such complex and detailed things, we will need to figure out how to break those problems down into manageable pieces to help us more easily understand the problem, program it, and debug it or modify it in the future.
Next -- we implement the prepare method - click on the tab in the lower right window (where you put your program) that says World.prepare. After that we'll go on to the zoom method. The storyboard for prepare is:
World.prepare
Do Together
Do Together
truck tilts up
truck moves up
windupKey turn 3 times
Do together
truck tilts down
truck moves down
As you create the code, you will have to play around to figure out which instructions and what parameters to those instructions give you a result that looks like the video. Give it a shot. Maybe you won't make the EXACT same code we did -- but try to get as close as you can.
Don't worry -- we'll give you our code. Just not yet :)
Be sure to Play your program to see if you are getting it right (we had to Play ours many, many times as we experimented trying to get the right methods and parameters -- probably 20 times or more!). Remember what you should expect to see: The instructions you have so far in the prepare method -- executed 3 times in a row! Why is that? Oh -- because in World.my first method the call to the prepare method is on a loop 3 times tile. Currently, nothing happens for zoom, since we haven't written that.
When you are done with prepare, move on to write the code for World.zoom. Here's the storyboard for that:
World.zoom
Do Together
truck move forward 1 meter
truck barrel turn 1 revolution
truck play whoosh2 sound
flashLightning
As you implement this recall that World.flashLightning is another method you need to create. We suggest you use create new method (be sure to click on the World object first) to make the flashLightning method -- but leave the method definition empty -- that is, don't bother to write the code yet. It's easier if you finish up and test the zoom method first. Make sure it's working -- just without the lightning effects -- before you go on to write the code for World.flashLightning.
When you are ready, go on to create World.flashLightning. The storyboard is:
World.flashLightning
Do Together
all lightnings become visible
Do Together
all lightnings become invisible
In this storyboard, we shortened up what will take multiple lines in your program. You have to direct each lightning separately. If that feels tedious and repetitious, don't worry -- in the next module we'll explore how the computer can help us do this with less work on our part!
When you think you are done and have compared what happens when you hit play to the original video -- download the image from the file section at the bottom of this page which has the code we created in it... How similar was yours to ours? Is there something in yours you like better?
When you are satisfied, go back to World.my first method. Change the number of time the loop runs -- maybe try 5, 9, 20. How powerful to be able to change one number and create so much?!?!
Once you have finished your code, you may view our code here. Make sure you try to get your code working as close to the video as possible first!
Save your World as Ch10-pg3-CementTruck-YourName.a2w