Instructor: Barbara
Lattanzi
Lingo Scripting for
Macromedia Director
revised 2/03
LINGO SUMMARY
3 types of scripts
we will be using this semester:
- movie script
- behavior script
- parent script - the 3rd type of script will get our separate attention.
Movie scripts
affect the entire movie. They don't get placed anywhere on the
score. They only appear in the cast member window.
Behavior scripts
can be attached to an individual sprite (a "sprite script"), to
a castmember (a "castmember script"), or placed in the frame channel
of the score (a "frame script").
You can have more
than one movie script.
It is common to have more than one behavior script attached to any
sprite. But you can also combine many event handlers and commands
into one sprite script.
A script contains
3 main parts:
- The event handler
is announced on line 1. It identifies the kind of event that will be the condition
for triggering the following lines of the script. It begins with the
keyword "on" and always ends with the keyword "end". Examples of event handlers
are "on mouseUp", "on
mouseDown", "on frameExit", "on
startMovie", etc.
When the script is either attached to a sprite or in the frame script
channel, add the keyword "me"
(for example, "on mouseEnter me",
"on exitFrame me", etc.). Do
not use the keyword "me" with movie scripts or with cast member
scripts.
A side note:
behavior scripts attached to cast members (rather than attached to sprites)
are of limited usefulness and, to avoid confusion, it is better to avoid
scripts for cast members completely. (Use only those behavior scripts
placed in the frame script channel or attached to sprites.)
- The commands
begin on line 2 for an unlimited number of lines. You can write any
number of lines of commands. Each command must be on its own line. Some
commands need more than one line. (For example, repeat loops
have to be broken up into several lines and have very specific line
breaks.) Examples of commands are "go
to the frame", "the
stagecolor = random(255)", "updateStage",
"puppetSound
2, "myloudsound" " (where "myloudsound"
is the particular name of your specific sound file and 2 happens to
be the number of the channel where your sound is played), etc.
- The final line
after the command(s) must be the keyword "end".
This simple word is all that is needed to tell the program that it is
done "handling" the particular event named in the first line.
(Some Lingo programmers like to add the type of event after the word
"end". For example, "on
mouseUp" script can end with the line "end
mouseUp". But, don't worry! Just using the word "end"
in the final line is enough!)
Lingo Hierarchy, how
script "conflicts" get figured out:
If you write a frame
script that uses the event handler "on
exitFrame", but then you write another script for a sprite
that is on the same frame as the frame script that also uses the
event handler "on
exitFrame" then what happens? Which event handler gets "listened
to"? In the event of such a conflict, the way Director sorts it out is
by prioritizing scripts. The hierarchy is to give priority to the sprite
scripts first, then cast member scripts, then frame scripts,
lastly movie scripts.
LINGO CAN MANIPULATE
PROPERTIES OF CAST MEMBERS, OF THE STAGE, AND OF
SPRITES.
EXAMPLE of a movie
script that sets the frame-rate (tempo or speed) of your movie, measured
in frames-per-second.
on startMovie
puppetTempo 15
end
EXAMPLE of behavior
script (that is, either a frame script put in the frame channel
of the score, or a sprite script attached to a sprite) that manipulates
the color property of the stage. (Remember, if the script
is attached to a sprite, add the keyword "me",
as in, "on mouseUp me"):
on mouseUp
the stageColor = 30
end
EXAMPLE of behavior
(frame) script that manipulates the location property of
the sprite (a sprite in channel 3 of the score) repeatedly as the
movie loops on the same frame. Try putting this script in the frame channel
of the score:
on exitFrame
sprite(3).loc = sprite(3).loc + 3
go to the frame
end
Note: you can also
attach the above script to any sprite. To do that you add a keyword
"me"
to the event handler. The keyword "me"
is the way that the sprite references itself (just like people). Try this
script again, this time attached to any sprite instead of putting it in
the frame channel. Remember to add the "me" keyword.
on exitFrame
me
sprite(3).loc = sprite(3).loc + 3
go to the frame
end
An important note
about the "me"
keyword (which will be used in the subsequent examples for sprite scripts):
Although you will
see the "me"
keyword used in the rest of these script examples for sprites, it is
true that many times you can forget using it and the script still works
just fine. Where the "me"
keyword becomes absolutely necessary whenever the sprite references
itself within any command. The way a sprite references itself in a command
is this way: "sprite(me.spriteNum)".
That way a sprite "knows" automatically in what sprite channel
number you have placed it.
EXAMPLE of a behavior
(frame or sprite) script that manipulates the horizontal location of a
sprite in channel 5. (Use the "me"
keyword, if you attach this to a sprite):
on mouseUp
me
sprite(5).locH = sprite(5).locH + 2
end
EXAMPLE of a behavior
(sprite) script that manipulates the horizontal location of a sprite in
any channel, and for any sprite you attach the script to. Why "any
sprite"? Because by indicating "sprite(me.spriteNum)" instead
of a specific channel number, you are using the general way of referring
to the sprite and allowing the sprite to figure out for itself what channel
number it is in.
(Note that this kind
of "self-knowing" of the sprite is a fundamental characteristic
of a computer-based system. Computer-based systems have the capacity for
"feedback". "Feedback"
is simply the system's ability to monitor itself in a particular activity
and to make changes based on that activity. When we use Lingo, we are
taking advantage of the system's built-in capacity for feedback.)
on mouseUp
me
sprite(me.spriteNum).locH = sprite(me.spriteNum).locH + 2
end
EXAMPLE of a behavior
(frame or sprite) script that changes the vertical location of a sprite
in channels 5, then returns that sprite to its original position. Put
these two events (mouseDown and mouseUp) into the same script
window and attach the script to a sprite:
on mouseDown
me
sprite(5).locV = sprite(5).locV + 2
end
on mouseUp
me
sprite(5).locV = sprite(5).locV - 2
end
Try subsituting "sprite(me.spriteNum)"
for "sprite(5)" in the above script. Then attach the script
to several different sprites.
2 EXAMPLES of a behavior
script attached to a sprite that changes the castmember that is associated
with that sprite:
on mouseEnter
me
sprite(me.spriteNum).memberNum = 14
end
on mouseLeave
me
sprite(me.spriteNum).member = "my cat"
end
EXAMPLE of a behavior
script attached to a sprite that "attaches" the sprite to wherever
the cursor is located by using "the
mouseLoc" property (i.e., the mouse's location).
Note that this script needs the movie to be paused on one frame (using
the command "go to the frame") for this to work:
on exitFrame
me
sprite(me.spriteNum).loc = the mouseLoc
go to
the frame
end
Example of a behavior
(frame or sprite) script which manipulates the text property of a Field
Cast Member (also Text Cast Member) by changing its text. To make
a "blank" Field Cast Member, open a field window under
the "windows" menu in Director, and just use the spacebar to
type a single space in the text area. This single space allows Director
to recognize that the Field cast member has content. IMPORTANT: for this
example, put the Field Cast Member location in slot 3 of the cast
window (or change the script to whatever slot number you have it in).
Now, drag the Field Cast Member on the stage. This script replaces
the text of that cast member. Attach this script to any image sprite on
the stage or put the script into the frame channel.:
on mouseUp
me
member(3).text = "My dog has fleas."
end
LINGO CAN BUILD UPON
THE RESULTS OF THE VIEWER/USER'S ACTIONS & LINGO CAN CONTROL REPEATING
EVENTS AND CHANGING OR UNPREDICTABLE EVENTS BY USING "FUNCTIONS",
"REPEAT LOOPS" AND "IF/THEN
CONDITIONAL LOGIC".
EXAMPLES OF A RANDOM
FUNCTION :
Example of a behavior
(frame or sprite) script that continuously changes the stagecolor property
by continously selecting a random color value between 1 and 255.
Note that the command "go to the frame" is also added here to
instruct the movie to pause at the frame. You aren't required to make
the movie pause, however, without the movie pausing, then you would not
see the stagecolor go through continuous changes.
on exitFrame
me
the stagecolor = random(255)
go to the frame
end
Example of a behavior (frame or sprite) script that randomizes
the vertical and horizontal location of a sprite in channel 7, while the
movie loops on the same frame. The command "go
to the frame" is what makes the movie continuously loop in
one frame. (Use the "me"
keyword, if you attach this to a sprite. Otherwise, put this script in
a frame channel. You should also have a sprite in channel 7 of the score.):
on exitFrame
sprite(7).locH = sprite(7).locH + random(3) - 2
sprite(7).locV = sprite(7).locV + random(4) - 3
go to the frame
end
Example of a behavior
(frame or sprite) script that randomizes the forecolor of a one-bit-depth
sprite. You can change the bit-depth of a cast member within Director
under the Modify > Transform Bitmap menu. (Use the "me"
keyword, if you attach this to a sprite):
on mouseUp
me
sprite(2).forecolor = random(255)
end
EXAMPLES OF A REPEAT
LOOP:
Repeat loops
are efficient and fun for multiplying things happening on the stage. One
rule-of-thumb: do not have more than one repeat loop script happening
at any one time. You can't have simultaneous repeat loop scripts.
Example of a behavior
(frame or sprite) script, while the movie loops on the same frame, for
a "sticky sprite" which uses a repeat loop to match the location of a
sprite to wherever the cursor is. It uses the helpful command "updateStage"
to make sure that the screen redraws itself immediately so that the sprite
moves smoothly. Try the following script once with, and then once
without, the line "updateStage".
(Note how differently the sprite moves when you include "updateStage"):
on exitFrame
me
repeat while the mouseDown
sprite(2).loc = point(the mouseH, the mouseV)
updateStage
end repeat
go to the frame
end
Example of a behavior
(frame or sprite) script that manipulates the rotation property
of 5 sprites in channels 5, 6, 7, 8, and 9. Make sure that you have sprites
those five channels. (Use the "me"
keyword, if you attach this to a sprite. You only need to attach it to
one sprite. Do not attach this script to all 5 sprites! See the rule-of-thumb
mentioned above!):
on exitFrame
me
repeat with i = 5 to 9
sprite(i).rotation = sprite(i).rotation + 1
end repeat
go to the frame
end
EXAMPLES OF IF/THEN
CONDITIONAL LOGIC:
Being able to describe
the specific conditions under which an event or a command can occur is
the heart of interactive multimedia programming. This means setting up
rules for events to occur, whether those events are person-driven or machine-driven.
When you set up conditions, you are using a logic system that forms the
basis for the kind of interactions possible between person and machine.
It works like magic!
Example of an if/then
conditional statement. (Use the "me"
keyword, if you attach this to a sprite):
on exitFrame
me
if member(3).text = "The score is 20." then
puppetSound 2, "trumpet"
alert "You won!"
go to frame "start over?"
else
go to the frame
end if
end
Here is a frame script
behavior that defines the conditions under which the image (on a stage
that is 320 pixels wide) will scroll from left to right. The condition
depends upon where the user's mouse is located.
on exitFrame
if the mouseH > 160 then
sprite(1).locH = sprite(1).locH - 1
else
if the mouseH < 160 then
sprite(1).locH = sprite(1).locH +
1
end if
end if
go to the frame
end
Note in this script
how the 2 "if"
statements have to end with 2 "end
if" lines. This is referred to as "nested" if/then
statements.
Try using the vertical
location of the sprite (the "locV"
property).
Try using the vertical
position of the mouse(the "mouseV"
property)
CHECK IF A SPRITE
IS BEING ROLLED-OVER BY THE MOUSE AND USE THAT TO TURN A SPRITE'S VISIBILITY
PROPERTY "ON" AND "OFF"
Here is a behavior
(frame or sprite) script which manipulates the sprite property of "visibility".
You can write a script that turns the visibility of a sprite "on"
and "off" depending upon if/then conditions which you
set. This script uses a sprite in channel 1 to make a sprite in channel
3 invisible, whenever the mouse rolls over sprite 1. [Note: the words
"true" and "false" are capitalized in the script for
clarity only. Capitalization is never required in Lingo scripts.
on exitFrame
me
if sprite(1).rollover = TRUE then
sprite(3).visible = FALSE
else
sprite(3).visible = TRUE
end if
go to the frame
end
PRESTO! CHANGE
A SPRITE'S CAST MEMBER BACK AND FORTH BETWEEN 2 DIFFERENT CAST MEMBERS
USING IF/THEN CONDITIONAL STATEMENTS.
A cast member is
a property of a sprite on the stage. In this example, sprite 1 is
derived from cast member 4 being dragged to the stage. Here is a sprite
script behavior which describes the conditions for which that cast member
is switched to a different cast member. What you see is the sprite do
a magic switch between 2 different images everytime you click on it. Attach
this script to the sprite that is doing this magic act:
on mouseUp
me
if sprite(1).member = 4 then
sprite(1).member = 5
else
if sprite(1).member = 5 then
sprite(1).member = 4
end if
end if
end
end of summary
|