AS3 Gotcha #2: MovieClip paths using root/_root and parent/_parent
Okay, so we know that in ActionScript 3.0, both _parent and _root have been changed to simply parent and root -- however, what we are officially told is that they have been ”inherited from the DisplayObject class”...whatever that’s supposed to mean. I take it we are supposed to spend some time learning the new metaphor that is the DisplayObject class. But what if you
Say you have a movie clip on the stage’s root (called body1_mc) with another movie clip inside of it (called heart_mc) and you wanted to access a different movie clip on the root (called body2_mc)—but you needed to do this from within heart_mc...?
In AS2, it would look like one of two things:
Relatively, in AS2:
this._parent._parent.body2_mc;
Or, absolutely, in AS2:
_root.body2_mc;
You’d think all you have to do in AS3 is remove the underscores in _root and _parent and you’d be all set. Just like with all the other MovieClip properties like _x, _y, _alpha, and _rotation, right? Well, I got news for you, my friend: you’d think wrong.
Instead of educating yourself on the intricacies of the DisplayList (for now), the quick solution for those of used to the way things were in AS2 would be to use Object(the path). This is what it would look like in our example:
Relatively, in AS3:
Object(this.parent.parent).body2_mc;
Absolutely, in AS3:
Object(root).body2_mc;
And now, things work as expected. Yay!
Note that body2_mc needs to fall
Alternatively, you could also say MovieClip(the path) but apparently it’s not such a good idea according to cancerinform over at the FlashKit forums. According to him:
It is better to use
Object(root).whatever. If you are dealing with a movie extendingSpriteor with both you will run into problems if you use either of them. If you know it is a Sprite or MovieClip only then useSprite(root)orMovieClip(root).
I also just discovered that Flash Speaks ActionScript has done a series of posts similiar to “AS3 Gotchas”! (Great minds think alike...) Here’s their take on this very subject: Getting Started with Understanding Paths Within AS3
About this entry
You’re currently reading “AS3 Gotcha #2: MovieClip paths using root/_root and parent/_parent,” an entry on ActionScript for the Pissed-Off Designer
- Published:
- 12.18.08 / 10pm
- Category:
- AS3 Gotchas
2 Comments
Jump to comment form | trackback uri