Sei sulla pagina 1di 2

___________________________________________________________________________________

Tutorial 4-3:
Using the Loop instruction
Here is the problem statement for the world you will create in this tutorial:

Create an Alice world with a clock that has a minute hand and an hour hand. The
clock should simulate the movements that a clock makes when keeping time. The
minute hand should make one revolution around the face of the clock, and then the
hour hand should advance to the next hour. (The movements do not have to occur in
real time. For convenience, the movements can occur at a faster rate of speed.) The
clock should make these movements for a total of 12 simulated hours.

A world named ClockLoop has been partially created for you to use in this tutorial,
and is on notes.cit.ie. The world has the clock object shown in Figure 4-34.

Figure 4-34 The clock object

The clock object has two subparts: minute (the minute hand), and hour (the hour
hand). When we play the world, we want to see a simulation of the movements that a
clock makes when keeping time. The minute hand should make one revolution around
the face of the clock, and then the hour hand should advance to the next hour. Here is
the pseudocode for the algorithm:

Loop 12 times
minute rolls left 1 revolution.
hour rolls left 1/12 revolution.
End Loop

The first instruction in the loop causes the minute hand to rotate left one complete
revolution. This simulates the passing of 60 minutes. The next instruction causes the
hour hand to rotate 1/12 of a revolution. This will cause the hour hand to advance to
the next hour position on the clock.

Step 1:Copy the ClockLoop world from notes.cit.ie and open it.

Step 2:Drag the Loop tile and drop it into the Method Editor. A menu appears for you
to select the number of times that the loop should repeat. Select other...
and then enter 12 on the Custom Number keypad. An empty Loop
instruction like the one shown in Figure 4-35 should appear.

1
___________________________________________________________________________________

Figure 4-35 The empty Loop instruction

Step 3:In the Object Tree, expand the clock object so you can see the tiles for its two
subparts, minute and hour.

Step 4:Drag the tile for the minute object's roll method and drop it inside the Loop
instruction. Select left for the direction and select 1 revolution (all the way
around) for the amount. The instruction tile should be created inside the
Loop.

Step 5:Drag the tile for the hour object's roll method and drop it inside the Loop
instruction, below the instruction that you created in Step 4. Select left for
the direction. For the amount, select other... and then in the Custom
Number keypad enter 1/12. When you click the OK button the instruction
tile will be created. (Note that the amount has been converted to 0.08.)
The Loop instruction should now appear as shown in Figure 4-36.

Figure 4-36 The completed method

Step 6:Save the world and then play it. You should see the clock make the simulated
movements of a real clock's hands, but in faster time. It might take a while
for the entire loop to complete all 12 iterations, so you can stop the
simulation at any time by clicking the Stop button.