Week 3
Repeat With, Repeat While, Lists [Arrays]
Repeat With
When you know there are a certain number of times you want to do something,
it may make sense to use a Repeat With loop.
Use an increment in this kind of loop with a counter variable
(conventionally "i" but it can be anything).
You set a start and end value. Each time through the loop, your counter variable
gets incremented by 1.
Example:
repeat with i = 1 to 10
sprite(i).rotation = sprite(i).rotation + 10
end repeat
Can save lots of time and space if you are doing the same thing to a bunch of sprites.
For instance, the below acts exactly the same as the example above. (And we are only
rotating the sprite - imagine if we were doing a bunch of things to it...)
sprite(1).rotation = sprite(1).rotation + 10
sprite(2).rotation = sprite(2).rotation + 10
sprite(3).rotation = sprite(3).rotation + 10
sprite(4).rotation = sprite(4).rotation + 10
sprite(5).rotation = sprite(5).rotation + 10
sprite(6).rotation = sprite(6).rotation + 10
sprite(7).rotation = sprite(7).rotation + 10
sprite(8).rotation = sprite(8).rotation + 10
sprite(9).rotation = sprite(9).rotation + 10
sprite(10).rotation = sprite(10).rotation + 10
When you want to do something until a certain condition has been met,
it may be a good time for a Repeat While loop.
Repeat While with time-based events
Repeat something for a specified period of time. Use the timer.
This will make the sprite bigger for 5 seconds:
on mouseDown
startTimer
repeat while the timer < 60 * 5
sprite(2).width = sprite(2).width + 1
updatestage
end repeat
end
updatestage explicity tells Director to redraw the stage. If Director is inside a repeat
loop and does not get to the go the frame loop, you may not see what you expect to see.
In that case, you may have forgoteen to include the updatestage command (very common mistake!).
Repeat While with action-based events
Repeat something while the user is taking some kind of action:
This will move the sprite while the user has the mousedown
repeat while the stilldown
sprite(1).loch = sprite(1).loch - 4
updatestage
end repeat
Whereas variables can hold one value,
lists can hold a whole bunch of values at once.
(They are called arrays in most other computer programming languages.)
This gets useful when you need to store a lot of information about the same kind of thing.
example
myTrialTimes = [6.7, 7.4, 8.2, 6.8. 7.1]
Initialize a list like this:
on beginSprite
myTrialTimes = []
end
Add things to a list like this:
myTrialTimes.add(6.7)
myTrialTimes.add(7.4)
Values get added to the END of a list.
Get and set values by using their order in the list:
put myTrialTimes[3] (message window would show 8.2)
myTrialTimes[3] = 8.0
put myTrialTimes
message window shows [6.7, 7.4, 8.0, 6.8. 7.1]
Access the values in a list in order with a Repeat with loop
If you lose track of how many values there are, find out using
listname.count
Example: Find an average of my trial times...
total = 0
repeat with i = 1 to myTrialTimes.count
total = total + myTrialTimes[i]
end repeat
average = total/myTrialTimes.count
Property Lists (Known as a hash table in other languages)
With property lists, you can name the positions using #.
Can be useful because you don't have
to remember the position of a value in the list.
example
myItalianDinner = [#primipiatti:penne, #secondipiatti:fish, #antipasto:prosciutto, #desert:tiramisu]
myItalianDinner.addprop = [#vino:chianti]
put myItalianDinner.antipasto
message window shows "prosciutto"
[You can still use order # if you want to. Could also have said put myItalianDinner[3]]
put myItalianDinner
message window shows [#primiPiatti:penne, #secondipiatti:fish, #antipasto:prosciutto, #desert:tiramisu, #vino:chianti]