Using MovieClip(root)

Once we begin to add objects to our projects, as in our encounter with collisions in Week 5, we need to know how one ActionScript object (say, a Movie Clip) can send information to another. This involves a very useful expression: MovieClip(root).

By default, an ActionScript class only has access to its own data and methods; but in order to detect collision, one class needs to derive information about a class outside itself.

Luckily for us, there is a way to obtain this missing information, by using a construct called root. For any Flash project, root represents the highest level in the data hierarchy, or the parent of all the components.

If we use root with dot notation, we can then access any object or property that is a subsidiary, or child, of root. This will include all other Movie Clips in the main movie.

However, there is a small catch involved: while our friend root is an object, it is a particular kind called a DisplayObject.

Display Objects are strange beasts. They were introduced in ActionScript 3.0 to resolve problems in earlier versions of ActionScript, concerning levels of abstraction. However, Display Objects are not especially useful in and of themselves. To make the Display Object effective, you must convert root into a Movie Clip, which you can do with our magical expression:

MovieClip() (read aloud as "Movie Clip paren-paren") is the conversion method of the MovieClip object. Any object or instance name placed within its parentheses is passed to the method to be turned from whatever it is, into a Movie Clip.

Most objects in ActionScript 3 have conversion methods, and will allow you to convert any equal or higher-order object, meaning any object that contains at least as much data as the object into which you are converting. Since a Display Object has more data than a Movie Clip, this conversion is valid.

Now, if Movie Clip is a subclass of Display Object (which it is), you might wonder why we need to convert at all. The answer is somewhat counter-intuitive: for some reason, while Display Objects contain all the data available to Movie Clips, they don't contain the same methods. That's why we have to convert.

Admittedly, this is all somewhat mysterious. It's not important that you fully understand the context of MovieClip(root) at this point. If Flash scripting is as far as you ever want to go with code, you can simply memorize MovieClip(root) as a kind of formula. It's the incantation, in effect, that lets you communicate between one Movie Clip and another.

Once you understand conversion (or once you've memorized the formula), all you need to know is how to use dot notation to address one Movie Clip from the script attached to another. It's really quite simple. Suppose I have two Movie Clips in my main movie, each with a distinct instance name. The first is called mc_A and the second is called mc_Q. I want to do something when these two Movie Clips intersect, or collide.

In the class code for Movie_A, write the following statement within, say, the callback from an ENTER_FRAME event:

We assume doMeSomething() is a properly defined custom method, taking care of whatever business has to happen when the two Movie Clips collide.

One word of caution: if you put the hit test method into the classes for both Movie Clips, you may end up with interesting and/or disastrous results. It's possible to script for mutual hit testing, but it raises complications we won't discuss here.

Our illustration covers hit testing, or collision, but in fact inter-object communciation via MovieClip(root) can be used in many other cases, and you will see it in almost every project as we go forward.




University of Baltimore Logo

Last updated: 09/23/09 16:17:38
Copyright © 2009 School of Information Arts and Technologies