AS1 OOP: Object Basics
by senocular
MovieClips as Objects
You’ve probably heard of movieclips being called movieclip
objects. The reason for that is that movieclips are
themselves just as much an object as any other object. They
have their own properties, can have added to those more
properties and can contain yet other objects or movieclips
within themselves which behave similarly. Basically the only
difference is that with movieclips, you can physically see
the object as it’s a visual object with a timeline. Other
visual objects include buttons and textfields, though
movieclips have a greater importance because they are the
only type of those visual objects that can have other nested
timelines (movieclips) within them. That is not the case so
much with buttons and textfields.
Movieclips are also a special case as they exist as soft
references. Because of a movieclip’s timeline and the fact
that its visually seen on the screen, it’s handled
differently from other objects. With other objects, if you
delete all or have no remaining reference variables to an
object in memory, Flash sees that object as non-existent and
forgets it ever existed (Actionscript handles garbage
collection on its own in that manner). However, the only
method of removing a movieclip is through using the
removeMovieClip() method. This method physically extracts
and removes the movieclip from the timeline it which exists.
Any references to that movieclip can still very well exist
in your Actionscript even though the movieclip itself does
not. These remaining soft references technically still point
to the movieclip object but do so blindly. All is not lost,
however, since if the movieclip returns to the stage, the
reference will be able to again correctly reference the
clip.
It’s like sending smoke signals to your best friend in a
far off land who doesn’t have a fire. You know you can give
him messages but he has no way of returning them. What
happens when your friend moves (or gets eaten by a horde of
rabid buffalo)? You would have no way of knowing that so you
would continue sending your smoke signals as though nothing
has changed even though no one is able to read them. Should
your friend ever come back, he will again be able to receive
those smoke signals.
Take a look at the following example.
- // create a temporary movieclip
- this.createEmptyMovieClip("temp",1);
- // assign a reference variable
- tempRef = temp;
- trace(temp); // traces _level0.temp
- trace(tempRef); // traces _level0.temp
- // remove the movieclip
- temp.removeMovieClip();
- trace(tempRef); // traces nothing
- trace(typeof tempRef); // traces "movieclip"
- // re-create the movieclip
- this.createEmptyMovieClip("temp",1);
- trace(tempRef); // traces _level0.temp
You can see there, that the tempRef variable, though the
movieclip was removed, remained defined in the script. It
had no base value when traced, though it still knew it was a
movieclip reference. When the movieclip was reattached
(using the same name as it was when originally created), the
tempRef variable again was able to correctly reference that
clip once again.