Date: Wed Nov 10 00:45:07 MST 1999 From: Brennan Young <brennan@young.net> Subject: Attach Behaviors on-the-fly>How on earth do I go about attaching behaviors to sprites on-the-fly?
I've now seen people asking about this several times, which means it's a FAQ, although I haven't seen a quick solution posted anywhere vaguely permanent.
Well, I've had a script floating around since Director6 (old syntax) which does this quite nicely, so I figure I might as well post it here on Alan's fine site.
The appendBehavior handler is intended to be put into a movieScript because it is of general utility, but you could also roll it into an object if you liked.
As it happens, I have used this handler only very rarely, and mostly only when someone asks about it out of curiosity. In practice, there are usually easier and more reliable ways to get the same results (make your behaviors switchable with an #enabled property or something), but it doesn't hurt to show the technique, especially if it silences a FAQ.
The appendBehavior handler should work with most behaviors, and is designed to use the default values returned by getPropertyDescriptionList, if such a handler exists.
Geek detail: I'm using call() with a list as the second parameter so that if the getPropertyDescriptionList handler is not defined, nothing untoward will happen. I do the same with beginSprite, because not all behaviors have that handler either.
This handler could also be used to instantiate and attach other script types such as parent scripts, although I have made no provision for custom 'new' handlers with extra parameters. This could be easily added, and I fully expect that people who are in the business of writing parent scripts with custom 'new' handlers would be able to work it out for themseleves. (Hint: Use the paramcount and param() function.)
This version is for people who just want to write one line of Lingo which does the same job as dragging a behavior onto a sprite and accepting any default property dialog parameters. Geeks can examine the code and comments for more technical information.
An example call, to attach the behavior 'Rollover Change Member' to sprite 3 would be
appendBehavior "Rollover Change Member", 3
And now on to our feature presentation...
on appendBehavior scr, spr
--you can pass types member
-- membername (string)
-- and script to the 'scr' param
set class to script(scr)
--instantiate object
set obj to new(class)
--specify spritenum property in the instance
setaProp obj,#spritenum, spr
--The default values from getPropertyDescriptionList
--(if it exists) are used to configure the behavior.
set initprops to call (#getPropertyDescriptionList, [class])
--do we have any initializers from getPropertyDescriptionList?
if ilk(initprops) = #proplist then -- assign them...
set thisMany = count(initprops)
repeat with x = 1 to thisMany
set thisProperty = getPropAt(initprops, x)
set thisValue = getProp(initprops, thisProperty)
setProp(obj, thisProperty, the default of thisValue)
end repeat
end if
--attach to the sprite
add the scriptInstanceList of sprite spr, obj
-- Call beginsprite, if it exists...
call #beginsprite, [obj]
end