[14.2] So why would I want to use them?
Because they're glamorous and fashionable, of course!
But as well as that, objects provide a number of features that otherwise
are a pain to achieve:
- they create "intelligent" data, which "knows" what can be done with it
- you can use a single command with different objects and have each object
interpret the command in a way appropriate to itself
- you can turn
handlers into global data which can be shared between movies
- you can keep
them in lists, changing them on the fly, and send them all messages
(commands or events) at once. A special case of this is the actorList, a
built in Director property that you can put as many objects as you want
into, and they'll all get sent a stepFrame message each time the movie
changes frame (or loops back on the same frame).
- they can inherit
handlers from other "ancestor" scripts, so you can share some handlers
between objects while letting them keep handlers specific to themselves.
One common use for objects in Director is to create sprites on the fly,
each of which takes care of its own behaviour independently. By using
different parent scripts to give different possible behaviours, you can
easily create movies which vary each time you play them.
While it is possible to do most of these things without using objects, it
can be a lot harder.
For example, say you have five kinds of sprite, all of them doing different
things each time the movie advances a frame (maybe moving a particular way,
or changing cast, or whatever). Any given sprite may be any of the five
types, since you randomly select them as you go along.
If you wanted to do this without objects, you'd have to have a handler
which went through all the sprites, checked what type they were and then
made them behave accordingly. It would probably involve lots of nested
if..then..else statements and generally be a bit of a nightmare.
With objects, however, you just have a different parent script for each
type, each with a stepMovie handler that performs the appropriate action.
Then, you add all the objects you create from these parents to the
actorList, and they sort it out for themselves.