Genevieve Young <[EMAIL PROTECTED]> wrote:
> Is there any difference between
> on beginSprite me
> and on new me?
Hi Genevieve,
BEHAVIORS
---------
When you use a behavior, both handlers are called. Most behaviors do not
have an "on new()" handler. If there is none, Director doesn't worry. If
there is one, Director executes the instructions in it. Anything that you
can do in an "on new()" handler can also be done in an "on beginSprite()"
handler.
The difference between the two is that when "on new()" is called, none of
the properties set in the getPropertyDescriptionList() handler are
available.
In an empty movie, create the following behavior and drop it on the stage:
property customProperty
on getPropertyDescriptionList(me)
tPropertyList = [:]
tPropertyList[ \
#customProperty] = [ \
#comment: "Custom property" , \
#format: #string, \
#default: "default value" \
]
return tPropertyList
end getPropertyDescriptionList
on new(me)
put #new, customProperty
end new
on beginSprite(me)
put #beginSprite, customProperty
end
When you run the movie, you should see this:
-- #new <Void>
-- #beginSprite "default value"
The behavior is instanciated (i.e. a specific block of computer memory is
allocated to hold the properties of the behavior) when the playback head
enters a frame where the behavior exists from a frame where it did not
exist. The behavior instance is created automatically at the moment the
sprite-to-which-the-behavior-is-attached "begins".
PARENT SCRIPTS
--------------
To create an instance of a parent script, you call...
myInstance = script("Name of script").new()
Your parent script does not need to have an "on new()" handler: Director
will simply create an instance if the handler is missing. If you do include
an "on new()" handler, then this must return a pointer to the instance:
on new(me)
-- do stuff
return me
end new
"me" contains the address in the computer's memory where information on the
instance is stored.
Generally speaking, instance created from a parent script are not attached
to sprites, so no #beginSprite event is sent to them. You need an explicit
Lingo call to create a new instance of a parent script.
MIXED SCRIPTS
-------------
Advanced coders may use the same script as a behavior and as a parent
script. Indeed, the only difference I can see between a parent script and a
behavior script is that you can't drop a parent script on a sprite. You can
create a new instance of a behavior script by calling its (non-existant)
new() handler.
You can find an example of this in the Text section of the Library Palette:
the Calendar behavior can be used as an instance, not attached to any
sprite, in order to do date calculations. Drag the script into the Internal
cast of a new movie, then type the following in the Message window:
dateChild = script "Calendar".(new)
today = dateChild.InternationalDate()
dateChild.Calendar_ToggleFullNames(TRUE)
put today, dateChild.GetWeekDay(today)
-- [#year: 2001, #month: 12, #day: 1] "Saturday"
Now create a text member, place it on the stage, drop the behavior on it,
accept the default properties, and run your movie: you get a calendar in a
sprite. In the first case, the new() handler is called, in the second, both
the new() and beginSprite() handlers are called. The same code is used in
two different ways.
I hope this helps,
James
[To remove yourself from this list, or to change to digest mode, go to
http://www.penworks.com/lingo-l.cgi To post messages to the list,
email [EMAIL PROTECTED] (Problems, email [EMAIL PROTECTED])
Lingo-L is for learning and helping with programming Lingo. Thanks!]