start page | rating of books | rating of authors | reviews | copyrights

Book HomeActionScript: The Definitive GuideSearch this book

15.4. The movieclip Datatype

Again let's revisit another esoteric topic, having mastered the fundamentals of ActionScript. In Chapter 13, "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 = new Array( ) and then set x = 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 set x = foo (which makes x a reference to clip foo), and then delete foo using removeMovieClip( ), and then create another clip named foo, the reference x will again be valid (it will point to the new foo clip).

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 x made reference to it. Furthermore, if you create a second movie clip named foo, the old foo and the new foo can exist simultaneously, although the old foo would 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.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.