Read More Ninja Action!

Getting the "parent" property to work in Actionscript 3. Mom? Dad?!

Hey everyone!

Today I'll be helping you with a quick tutorial on using the "parent" property in Actionscript 3. If you're used to how things worked in AS2 you know that you can no longer refer to the parent of a clip with this simple notation:
The compiler is more strict and so they've changed the syntax a bit. I want this to be a beginner's tutorial so I won't go into detail about that, let's work on the new way to do things!

First, I've made a movie with a clip on the timeline. I wrote a function on the mail root level of the timeline that looks like this:
function traceParent():void
trace("You've succesfully called on the parent!");
Easy, just a trace statement to see if what we code inside the clip is correct. Now, put a clip on the stage and INSIDE the clip put this script:
myButton.addEventListener(MouseEvent.CLICK, callOnParentClip);

function callOnParentClip(ev:MouseEvent)
(this.parent as MovieClip).traceParent();
Pretty easy! Above the content inside this clip, make sure you make a button called myButton to call this function. The key line is here:
(this.parent as MovieClip).traceParent();
You can see that now we have to cast the clip as a MovieClip specifically. What about if we have a clip more than one level deep? This is how you would handle that:
(this.parent.parent.parent as MovieClip).traceParent();

Very simple. Check out the source below for an example clip using this technique, and happy coding!

Sample (AS3, Flash CS4)