This tutorial exercise provides step-by-step directions for building a simple animation that will guide you through using the different Alice loop control structures: count, for_each_in, and while. Here you will explore the basic construction of these control blocks and how to modify them to smooth out looping animations. To do this you will animate a spinning carnival tea cup ride. The ride is a spinning ride that has three components: a large platform that spins slowly, smaller platforms that whip the riders that spin one after another, and finally the teacups that spin the whole time the ride is running.
You will need to access other printed materials or have access to the alice.org website to view the how to content called out within these materials. These materials can be downloaded and printed for offline use from Alice.org
Don’t forget to save your projects frequently.
Setting Up the Scene
We have provided a constructed scene for you to use for this exercise that looks like the image found above. You will need to download it from the Alice.org website on the page associated with this exercise.
1. Start Alice.
2. Open from file and browse to where you saved teaCupStarter.a3p
You will see the ride already constructed. For the program to work as we move forward we have already grouped some objects so they will move together. The hare has been vehicled to one of the teacups. The teaCups have been vehicled to the teaTrays and the teaCup platforms have been vehicled to the disc. You can explore this in the scene editor by selecting an object and viewing the properties panel to see the setVehicle state.
Planning Your Program
First let’s outline our program from the following script:
The Tea cup ride has a large platform that spins clockwise 3 times for each ticketed ride.
Each time the large platform spins the 3 smaller platforms each spin one after another 1 turn counterclockwise.
While the ride is running the 12 teacups spin clockwise.
Let’s flowchart “The Tea cup ride has a large platform that spins clockwise 3 times for each ticketed ride”. For this we are going to use a count loop that sets the number of times the large platform spins.
Next let’s break down the next component “Each time the large platform spins the 3 smaller platforms each spin one after another one turn counterclockwise.” Since we want these to happen at the same time the large platform is spinning we want to have this new functionality inside a do together with the big wheel spinning. Instead of listing out each smaller wheel and programming it to spin we will make use of the for each in_ loop. To do this we have it loop through a simple turn procedure replacing the object for each of the discs until all have been used.
Finally let’s diagram “While the ride is running the 12 teacups spin clockwise.” To do this we will create a variable that tracks when the ride is running that we can turn on and off. We will then create a while loop that looks at this variable to determine if the small tea cups should be spinning. Finally we will create a condition that turns the variable off when the large disc stops spinning.
Using the count control structure
Let’s start by animating the large platform spinning: The Tea cup ride has a large platform that spins clockwise 3 times for each ticketed ride. For the following steps, you may wish to watch the Videos or check the Quick Reference Guides for the How To: Using Control Structures Overview
3. Select the count control structure and drag it into your program and set the count to 3.
4. Select the Disc and drag a turn statement into myFirstMethod and set it to right (clockwise) and 1 for a full rotation.
5. Play your program and observe.
6. To remove the stutter you see when each rotation completes we will use the animation style detail to smooth out the animation.
a. Select add details animation style on the turn statement
b. Select begin and end abruptly
7. Save and Run your world
If you have not learned the for each in_ or while loop you can plan and implement the smaller disc and teacup spins using nested do togethers with the count loop. using the following algorithm:
Count loop (3)
Disc turn Right 1 rotation duration 3 seconds
Do in Order
teaTray turn Left 1 rotation duration 1 second
teaTray2 turn Left 1 rotation duration 1 second
teaTray3 turn Left 1 rotation duration 1 second
Count Loop (3)
teaCupHatter turn Right 1 rotation duration 1 seconds
teaCupPlayingCard Right Left 1 rotation duration 1 seconds
teaCupMarchHare turn Right 1 rotation duration 1 seconds
teaCupCheshire turn Right1 rotation duration 1 seconds
teaCupHatter2 turn Right 1 rotation duration 1 seconds
teaCupPlayingCard2 turn Right 1 rotation duration 1 seconds
teaCupMarchHare2 turn Right 1 rotation duration 1 seconds
teaCupCheshire2 turn Right 1 rotation duration 1 seconds
teaCupHatter3 turn Right 1 rotation duration 1 seconds
teaCupPlayingCard3 turn Right 1 rotation duration 1 seconds
teaCupMarchHare3 turn Right 1 rotation duration 1 seconds
teaCupCheshire3 turn Right 1 rotation duration 1 seconds
Using the for each in_ control structure
Next let’s implement the smaller discs spinning using the for each in_ loop construct: Each time the large platform spins the 3 smaller platforms each spin one after another 1 turn counterclockwise. Remembering the flow chart above we will need to put this functionality inside a do together.
8. Drag a do together block inside the count loop so that the tea trays can spin while the bigger disc is spinning
9. Add your Disc turn statement into the do together loop
10. Drag a for each in_ control structure into the do together
11. In the pop up for the for each in_ select the following
a. Set the Item Type > Gallery Object > teaTray
b. Set the item name to eachTeaTray
c. Select Array > Custom Array and add each of the teaTrays to the Array
12.Select one of the teaTrays and drag a turn left (counterclockwise) 1 (full rotation) statement into the for each in_ block
13. Drag the eachTeaTray variable to replace the teaTray in the statement
14. Save and Run your world
You will notice that the small discs continue to spin after the large disc has stopped. This is because the durations for the three small discs spinning at the default 1 second add up to 3 seconds while the large disc only takes 1 second.
15. Select add details on the disc turn statement and select duration and set to 3 seconds
16. Save and Run your world
Using the while control structure
Next let’s implement the teaCups spinning using the while loop construct: While the ride is running the 12 teacups spin clockwise. Remembering the flow chart above we will need to put this functionality inside a do together with the other code you have created so far. We will also use a for each in together block to save us from having to write code for all 12 teaCups. Remember that the for each in together is a way to implement a do together for a whole set of objects that will be doing the same thing.
17. Drag a do together block into your program and drag the existing do together block inside of it.
18. Drag a while control structure into the do together
19. In the pop up select True
20. Drag a each in together into the while loop
21. In the pop up for the for each in_ select the following
a. Set the Item Type > Gallery Object > teaCup
b. Set the item name to eachTeaCup
c. Select Array > Custom Array and add each of the teaCups to the Array
22. Select one of the teaCups and drag a turn right (clockwise) 1 (full rotation) statement into the each in together block. If you want the teaCups to spin smoothly select add details animationstyle and select begin and end abruptly.
23. Drag the eachTeaTray variable to replace the teaTray in the statement
24. Save and Run your world
You will notice that the teaCups will just continue to spin forever even after the other components have stopped moving. This is because the while loop is always true. We will need to make and use a variable to turn the ride on and off.
25. Drag a variable.. block to the top of your program
a. Set the type to Boolean (True/False)
b. Name it rideIsRunning
c. Initialize it to True so that when your world starts the ride comes alive
26. You can now set the True in your while block to the variable you created for rideIsrunning by either dragging it from the variable statement or by accessing it in the drop down for the True in your while statement (select the little arrow next to the True)
We want the ride to turn off after the count loop for the large disc and the smaller trays. To do this we need to have the variable set to false after that loop is completed. If we just assign the variable afterwards it will be inside the same do together block we put the while loop in and will execute at the same time as the count loop. To enforce it executing after the count loop we need to use a do in order block to make sure the count loop completes before we turn the ride off.
27. Drag a do in order block block above the count loop and add the count loop inside the do Together
28. Drag an assign block after your count loop block inside the do together and select rideIsrunning and False. This will turn off the while loop when executed.
29. Save and Run your world
Adjusting the Durations
Because we set up the while loop you can experiment with changing the number of the count and you can observe that the program is smart enough to now continue the teaCups spinning to match!
You can also play around with the durations of the other components to speed up and slow down the ride! Remember that the Disc and the teaTray durations need to be coordinated at a 1:3 ratio.