Chapter 16
396
Using handlers
As described in “Using messages to identify events” on page 394, Director sends messages to
handlers within scripts when specific events occur. You attach a set of handlers to an object by
attaching the handlers’ script to the object. See “Creating and attaching scripts with the
Script window” on page 412.
Each handler begins with the word
on
followed by the message that the handler should respond
to. The last line of the handler is the word
end
. You can repeat the handler’s name after
end
, but
this is optional.
When an object receives a message that corresponds to a handler attached to the object, Director
runs the Lingo statements within the handler. For example, the
mouseDown
message indicates that
the user clicked the mouse button. To indicate in your script that an action should be performed
when the mouse is clicked, you include a line in your script that begins with
onMouseDown
. You
follow this line with the Lingo statements that should execute when the script receives the
mouseDown
message.
Using arguments to pass values to a handler
By using arguments for values, you can give the handler exactly the values that it needs to use at a
specific time, regardless of where or when you call the handler in the movie. Arguments can be
optional or required, depending on the situation.
To create arguments for a handler:
•
Put the arguments after the handler name. Use commas to separate multiple arguments.
For example, the following handler, called
addThem
, adds two values it receives in the arguments
a
and
b
, stores the result in local variable
c
, and uses the Lingo term
return
to send the result back
to the original handler:
on addThem a, b
-- a and b are argument placeholders
c = a + b
return c
end
When you call a handler, you must provide specific values for the arguments that the handler
uses. You can use any type of value, such as a number, a variable that has a value assigned, or a
string of characters. Values in the calling statement must be in the order they follow in the
handler’s arguments, and they must be surrounded by parentheses.
The following statement is a calling statement for the
on addThem
handler:
set mySum = addThem(4, 8)
Because
4
is first in the list of arguments, Lingo substitutes it for
a
in the handler. Likewise,
because
8
is second in the list of arguments, Lingo substitutes
8
for
b
everywhere in the handler.
After the calling statement sends these parameters to the handler, the handler returns the value
12
,
which corresponds to the variable
c
inside the
on addThem
handler. The variable
mySum
in the
calling statement is then set to
12
.
You can also use expressions as values. For example, the following statement substitutes
3+6
for
a
and
8>2
(or
1
, representing
TRUE
) for
b
, and would return
10
:
set mySum = addThem(3+6, 8>2)
Содержание Director MX
Страница 1: ...Using Director MX Macromedia Director MX ...
Страница 12: ...Contents 12 ...
Страница 156: ...Chapter 4 156 ...
Страница 202: ...Chapter 6 202 ...
Страница 244: ...Chapter 7 244 ...
Страница 292: ...Chapter 10 292 ...
Страница 330: ...Chapter 12 330 ...
Страница 356: ...Chapter 13 356 ...
Страница 372: ...Chapter 14 372 ...
Страница 442: ...Chapter 16 442 ...
Страница 472: ...Chapter 18 472 ...
Страница 520: ...Chapter 19 520 ...
Страница 536: ...Chapter 20 536 ...
Страница 562: ...Chapter 23 562 ...
Страница 566: ...Chapter 24 566 ...
Страница 602: ...Chapter 27 602 ...