Actionscript 3 parent reference.

This is a very short line of Actionscript 3, but its one that I’ve had copied to Stickie on my Desktop forever, so in the interest of cleaning up some, I’ll post it here on the blog in the Code Tips category. I think I’ve memorized at least this much AS3 now too =)

Here it is (queue drumroll) …

MovieClip(parent).the_mc.gotoAndStop(2);

Booyeah! Big font…. ok lets explain how this is used. Suppose the instance name the_mc refers to a movieclip on the main timeline. That line of code is being run from inside of another movieclip clip on the timeline (which doesn’t even need to have an instance name). So that nameless movieclip is just referring back to the main timeline with MovieClip(parent). then finding the_mc and telling it to go to frame 2 within its timeline. You can download an example file here…. parent_example_in_as3.zip . Of course you aren’t limited to just using gotoAndStop(2); at the end, you can change the movieclip’s  x, y, scaling, etc, etc,  using the usual Actionscript 3 code for that.

Also if you were trying to do the same thing inside of a moviclip inside of another movieclip, you’d use…

MovieClip(parent).MovieClip(parent).the_mc.gotoAndStop(2);     

Or you can skip using the_mc altogether and just get back to the main timeline and tell it where to go with those gotoAndStop statements. 

In Actionscript 2 we could have done the same thing by writing… 

_parent.the_mc.gotoAndStop(2);

or even lazier by just using

_root.the_mc.gotoAndStop(2);

And if you need more Actionscript 3 training, here’s one source. 


6 Responses

  1. You are the man, you save my life!! I stuck at this point for a few days, I thought there’re addChild and removeChild script problems, thank you very much!

  2. You can do the same with the_mc.parent.gotoAndStiop(2) or also if the mc in the mc the_mc.parent.parent.gotoAndStop(2);

    And thats real AS3.

  3. Is your Actionscript 3 set to Strict Mode? If not, that probably does work fine.

  4. Very good thx you ! : )

  5. I created a drop down menu inside a movie clip where the sub buttons when clicked on will load an swf or img into another mc that is on the main timeline. I have it working on all buttons EXCEPT it does not swap out the old swf with the newly clicked on swf button. It stacks them on top of one another. I am trying to do remove child but comes up with errors… where and how do i remove the child so that they do not stack on top of the other..etc…please adivse. Here is the code.

    mysubBtn.addEventListener(MouseEvent.CLICK, load2MC);

    function load2MC(event:MouseEvent):void
    {
    var swfLoader:Loader = new Loader();
    var theURL:String = event.target.name + “.swf”;

    var swfRequest = new URLRequest(theURL);

    swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
    swfLoader.load(swfRequest);
    function onComplete(evt:Event)
    {
    (root as MovieClip).mc_contain3.addChild(swfLoader)

    }
    }

    Thank you for any help you may have for this.

  6. That was so helpful, I was trying to use this :
    parent["stage"].nextFrame() ;
    because referencing Movies and also Variable was possible in that way but didn’t work to control the timeline. So thank you so much.

Leave a Reply