Chapter 16
420
Creating the on new handler
Each parent script typically uses an
on new
handler. This handler creates the new child object
when another script issues a
new(script
parentScriptName
)
command, which tells the
specified parent script to create a child object from itself. The
on new
handler in the parent script
can also set the child object’s initial property values, if you want. The
on new
handler always starts
with the phrase
on new
, followed by the
me
variable and any arguments being passed to the new
child object. See
new()
in the Lingo Dictionary.
The following is a sample
on new
handler:
property spriteNum
on new me, aSpriteNum
spriteNum = aSpriteNum
return me
end
This handler creates a new child object from the parent script and initializes the child’s
spriteNum
property with the value passed to it in the
aSpriteNum
argument. The
return me
statement returns the child object to the handler that originally called the
on new
handler. For
more information about calling the
on new
handler, see “Creating a child object” on page 421.
Adding other handlers
You determine a child object’s behavior by including in the parent script the handlers that
produce the desired behavior. For example, you could add a handler to the code above to make
the sprite change color.
The following parent script defines a value for the property
spriteNum
and contains a second
handler that will change the
forecolor
property of the sprite.
property spriteNum
on new me, aSpriteNum
spriteNum = aSpriteNum
return me
end
on changeColor me
spriteNum.forecolor = random(255)
end
Using the me variable
Typically, one parent script creates many child objects, and each child object contains more than
one handler. The term
me
is a special parameter variable. It must always be the first parameter
variable stated in every handler definition in a parent script.
The
me
variable tells the handlers in the child object that they are to operate on the properties of
that object and not on the properties of any other child object. This way, when a handler within
a child object refers to properties, the handler will use its own child object’s values for those
properties.
This is why it is always important to define
me
as the first parameter for parent scripts and to pass
the same parameter if you need to call other handlers in the same parent script, since these will be
the handlers in each of the script’s child objects.
Содержание DIRECTOR MX-USING 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...