Loops repeat a certain piece of code for a certain time, until a condition is met, or they may repeat forever.
Frame Loops / Looping in a Frame
We've already used the fact that Director loops within a frame to create, for example, the illusion of motion. example.dir
property p_clicked
-- when the viewer/user clicks on the sprite for the
first time p_clicked = 1 - the mostion starts.
-- If they click the second time p_clicked switches to 0 and the motion stops
on mouseDown me
if p_clicked = 1 then
p_clicked = 0
else
p_clicked = 1
end if
end
-- this is the loop, it happens
everytime the playhead exits the Frame (on exitFrame)
on exitFrame me
if p_clicked then
sprite(me.spriteNum).rotation
= sprite(me.spriteNum).rotation +10
else
sprite(me.spriteNum).rotation
= sprite(me.spriteNum).rotation
end if
end
Exercise: This circle will
just grow and grow and grow if you don't build some code that makes it stop
on click.
Below the loop script that makes it grow:
on exitFrame me
sprite(me.spriteNum).rect = sprite(me.spriteNum).rect
+ rect(-1,-1,1,1)
end
Repeat Loops
The command "repeat" can make loops that are not dependent on the
looping of the playhead within one frame.
In behavior scripts they can for example be used to check for changes.
If the loop is extremely elaborate and runnning all the time then it may interfere
with audio playback
In this example, once
all the sprites have been changed to red by the user, the user is transported
to the next scene.
The sequence in which the sprites were changed does not matter - when the last
one is changed, the playhead advances.
How do we know when all of them have been changed?
Each time the user clicks one of the sprites, it changes it's member and then iterates through the others to check if there are still unchanged ones.
Iteration is done by loop:
the following script just changes the member of the sprite
then it loops through all the sprites and prints their member number into the message window
on mouseUp me
sprite(me.spriteNum).member = 2
repeat with i = 1 to 9
put sprite(i).member
end repeat
end
of course, this is not enough, we need to add some conditionals
so that we can control what the script does when it finds out that NOT all sprites
have been changed.
on mouseUp me
sprite(me.spriteNum).member = 2
repeat with i = 1 to 9
if sprite(i).member <>
member(2) then
exit
repeat
end if
end repeat
end
The "<>" is the opposite of "=" and means "is
not equal". These are called Comparison operators.
This script checks the member number of each sprite. The code is repeated 9
times. Each time, the variable i is one number higher than before. So the first
time the loop is executed, the script checks sprite(1), then sprite(2), sprite(3)
etc.
When the script finds a sprite(i) that has not been changed to member(2), it jumps out of the repeat with "exit repeat" and does nothing else.
But what if we want it to actually jump forward when it finds that that it has arrived at the ninth iteration of the loop and finds that all the sprites have changed to member(2) ?
on mouseUp me
sprite(me.spriteNum).member = 2
repeat with i = 1 to 9
if sprite(i).member <>
member(2) then
exit repeat
else if sprite(i).member
= member(2) and i = 9 then
go to marker("scene2")
end if
end repeat
end
Like this you could for example do a simple memory game example
Here is a snippet out of the memory game (the whole thing would be too complicated to cover in an example).
example (with 8 parts)
example (the same with
4 parts)
on mouseUp me
repeat with i = 1 to 8
if sprite(i).member <>
member(3) then
counter = counter + 1
end if
end repeat
if counter >= 2 then
repeat with i = 1 to 8
sprite(i).member = member(3)
end repeat
counter = 0
end if
sprite(me.spriteNum).member = member(2)
sprite(me.spriteNum).ink =136
end
Sprite within... / intersects properties (this does not have anything to do with looping - but might be useful)
on exitFrame me
if sprite sprite(2) within sprite(me.spriteNum) then
member(4).text = "perfect"
else
member(4).text = "drag
the circle into the rectangle"iiiiiiii
end if
end
The "sprite...intersects" property checks if one sprite touches the
other. .
(Note: i am not using the dot syntax here because i think that this particular
property is easier to understand in the verbose syntax)
example
on exitFrame me
if sprite sprite(2) intersects sprite(me.spriteNum)
then
member(2).text = "OUCH!
NOT THAT CLOSE!"
else
member(2).text = "DRAG
THE CIRCLE CLOSER TO ME!"
end if
end
Joystick Xtra
download from here http://pro.wanadoo.fr/freextras/index_en.html#JoystickXtra -- this one only works on PC / there are some non-free ones that work on both mac / pc
Drop the Xtra in the Xtras folder which can be found in the Director Application folder.
it comes with an example that lets you test the functionality of your joystick / gamepad / Dance PAd
if you have a playstation 2 dance pad there is a usb adapter for pc
Jeff will do a Joystick demo in class on monday...