movieclip Datatype
Again let's revisit another esoteric topic, having mastered the fundamentals of ActionScript. In "Movie Clips", we learned that movie clips behave, for the most part, exactly like objects. However, movie clips are not just another class -- they are their own distinct datatype. Gary Grossman, the creator of ActionScript, explains the difference between the internal implementation of the movieclip and object datatypes as follows:
Movie clips are implemented separately from objects internally in the Player, although both manifest almost identically in ActionScript. The primary difference lies in the way that they are allocated and deallocated. Regular objects are reference-counted and garbage-collected, whereas the lifetime of movie clips is timeline-controlled or explicitly controlled with the duplicateMovieClip( ) and removeMovieClip( ) functions.
If you declare an array using
x=newArray( )and then setx=null, ActionScript will immediately detect that there are no remaining references to the Array object (i.e., no variables referring to it), and garbage-collect it (i.e., free the memory it used). Periodic mark-and-sweep garbage collection eliminates objects containing circular references. (That is, advanced techniques are used to ensure that memory is freed when two unused objects refer to each other.)Movie clips don't behave the same way. They come into and go out of existence depending on the placement of objects on the timeline. If they are created dynamically (e.g., with duplicateMovieClip( ) ) they are disposed of only when removeMovieClip( ) is used on them.
References to objects are pointers (memory address references); reference-tracking and garbage-collection protect the user from dangling pointers and memory leakage. References to movie clips, however, are soft references -- the reference actually contains an absolute target path. If you have a movie clip named
foo, and then setx=foo(which makesxa reference to clipfoo), and then deletefoousing removeMovieClip( ), and then create another clip namedfoo, the referencexwill again be valid (it will point to the newfooclip).Regular objects are different -- the existence of a reference to an object prevents that object from being removed in the first place. So if movie clips were objects, removeMovieClip( ) wouldn't remove the object from memory so long as the variable
xmade reference to it. Furthermore, if you create a second movie clip namedfoo, the oldfooand the newfoocan exist simultaneously, although the oldfoowould no longer be rendered.So, a separate movieclip type is appropriate because of its important differences from the object type. For similar reasons, the typeof operator reports
"function"for functions, even though functions are also akin to objects in many ways.