Inter-behavior Communication
Global variables are one way scripts can share data and communicate with
each other. Another form of communication can be the sending of messages
with arguments to behaviors using the sendSprite, sendAllSprites
and call commands.
The sendSprite command sends a message to all scripts attached
to a specified sprite.
Syntax: sendSprite (whichSprite, #customMessage,
arguments)
The sendAllSprites command sends a message
to all scripts attached to all sprites.
Syntax: sendAllSprites (#customMessage, arguments)
With both of the above commands, if none of the sprites’ behaviors have
a handler that corresponds to the message, the message will pass down the
hierarchy to the cast member script, the frame script and then the movie
script.
The call command sends a message to a specific behavior. Unlike
the sendSprite and sendAllSprites commands,
using call will not pass the message to cast member scripts,
frame scripts or movie scripts.
Syntax: call (#customMessage, script, arguments)
The customMessage is replaced with the message and arguments
is replaced by any values for parameters of the message to be sent. The
symbol (#) operator must precede the message.
The call command is often used together with scriptInstanceList.
The scriptInstanceList is a sprite property in the form of
list of behaviors attached to a sprite. The property is available while
the movie plays, when the movie is not running, the list is empty.
Syntax: sprite(whichSprite).scriptInstanceList
Example 1
The example below sends the custom message mChangeDirection
with the argument of #left to all behaviors attached to sprite
1. You will see this behavior in context a little lower down in this technote.
on
mouseUp
me
sendSprite (1, #mChangeDirection,#left)
end
We could have sent the custom message to a specific behavior instance. If we knew
the message was included in behavior instance 1 of sprite 1, we could write the following:
on
mouseUp
me
-- get the reference
of first behavior of sprite 1
-- i.e. the first
item on the scriptInstanceList
xref
=
getAt (sprite(1).scriptInstanceList,
1)
-- run the mChangeDirection
handler in the
-- referenced script,with a parameter
value of #left
call (#mChangeDirection,
xref, #left)
end
Example 2
on
keyDown
case the keyCode of
121 : sendSprite (10, #mouseUp)
116 : sendSprite (11, #mouseUp)
end
case
end
The above script checks if the pageUp or pageDown
keys have been pressed by identifying each key's numerical value (the
keycode). If the keycode is 121
(pageUp), the mouseUp
event attached to sprite 10 is activated. If the keycode
is 116 (pageDown), the mouseUp
event attached to sprite 11 is activated. No arguments are sent with the
message. This script could be used in a situation where you have a slide-show
with forward and back buttons (sprites 10 and 11), and where pageDown
is linked to the forward button and pageUp is
linked to the back button.
Changing the parameter value in a behavior using sendSprite
Below is a simple automatic animate behavior.
If you drag it onto a sprite, it will make a sprite move up or down, left
or right depending on what its direction property (as defined by pMyDirection)
has been set to.
--
define properties
property
spriteNum, pMyDirection,
pMyMoveDistance
-- event handlers
on
beginSprite
me
pMyMoveDistance
= 1
end
on exitFrame me
mMove
me,
pMyDirection
--
activates custom message mMove with 2 parameters
--
me and pMyDirection
end
--
custom handlers to allow change of direction
-- (in separate behavior)
on mChangeDirection me,
newDirection
pMyDirection = newDirection
end
-- custom handlers
to move sprite within this behavior
on mMove
me, whichDirection
case pMyDirection of
#up:sprite(spriteNum).locV = \
sprite(spriteNum).locV - pMyMoveDistance
#right:sprite(spriteNum).locH = \
sprite(spriteNum).locH + pMyMoveDistance
#down:sprite(spriteNum).locV
=
\
sprite(spriteNum).locV
+ pMyMoveDistance
#left:sprite(spriteNum).locH
=
\
sprite(spriteNum).locH
-
pMyMoveDistance
end case
end
-- prompt user for
parameter value
on
getPropertyDescriptionList
pList
= [:]
addProp pList, #pMyDirection,
[#comment: \
"What direction should I move?", #format:#symbol, \
#default:#left, #range:#stationary, #left, #right, #up, #down]
return
pList
end
Note: The continuation symbol (
\ ) was used above to split a statement over 2 lines but still treat it
as a single entity. This was done here because it would not fit into the
width of this page layout.
We can create a button that sends a message to this behavior to change the direction. For example, the following
will change the direction of the animated sprite to move left.
on
mouseUp
me
sendSprite (1, #mChangeDirection,#left)
end
The above could have been written as:
on
mouseUp
me
sendSprite (1, #mChangeDirection,me,
#left)
end
But the me is not essential in this situation.