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. 


Advertisements

21 thoughts on “Actionscript 3 parent reference.

  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. sguenther says:

    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. 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.

  4. Monse Adwan says:

    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.

  5. Jeremy says:

    I have a similar to scenario to what you are describing. I want to go from the main timeline to a frame inside a mc.

    The mc instance name is mcMod1 and the frame I want to go to is labeled m1page2

    I have tried the following with no luck

    mcMod1.gotoAndStop(“m1page2”);

    Any help would be greatly appreciated.

    Thanks in advance

  6. Jeremy says:

    I did everything you mentioned. When I test it, I get the following error:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at ProbabilityTutorial_fla::mcGoTo_7/fncGoTo()[ProbabilityTutorial_fla.mcGoTo_7::frame1:13]

    Here is the AS3 code I am using

    //GoTo

    btnGoTo.addEventListener(MouseEvent.CLICK, fncGoTo);

    var iGoTo:String;

    function fncGoTo(event:MouseEvent):void
    {
    iGoTo = txtGoTo.text;

    if (iGoTo == “XGbDwm”)
    {
    MovieClip(parent).mcMod1a.gotoAndStop(“m1page2”);
    }
    }

    The MC I am trying to go into is named mc_Module1 with an instance name of mcMod1a. The frame I am trying to go in the MC is labeled m1page2.

    Thanks again for your help.

  7. That error still means it can’t find the movieclip. Did you try…

    MovieClip(parent).MovieClip(parent).mcMod1a.gotoAndStop(“m1page2”);

    But you shouldn’t need to do that if btn.GoTo and mcMod1a are on the same timeline. Are they?

  8. Jeremy says:

    I have the btnGoTo in a MC in frame 1 of the main timeline. The mc_Module1 is in frame 4 of the main timeline.

    When I tried the code you just gave me I got the following error:

    1120: Access of undefined property mcMod1a.

    Thanks again.

  9. channing millerd says:

    the movie clip i am in is pyramidMC and the button in this movie is called webMC i need to play a movie clip mainMC.pyramidMC.wegalleryMC.webgal2MC . but i am struggling to get it to work . plz help.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s