AS2 vs. AS3: _root or .parent- Communicating Between SWFs

ok, so climbing up your hierarchy tree was soooooo easy in AS2. But, of course, in AS3 it isn’t easy at all.

Let’s say my master movie loads in an external SWF. OK, I’m chillin and hangin out writin my code in my external SWF when BAM!!!! All of a sudden i need some info or to call a function i already wrote in my master movie.

In AS2 you would do this:
_root.onOff = true; //changes the onOff variable in master to true;
_root.subMenuOnOff(); //calls my subMenuOnOff() function in the master;

In AS3 you do this:
MovieClip(this.parent.parent).onOff = true;
MovieClip(this.parent.parent).subMenuOnOff();
//this is bascially saying, i want to go to this object’s parent(loader), then i want to go to the loader’s parent (root timeline), and BTW the root timeline in a movieclip. I’m not sure why i have to tell you that when you can’t be anything other then a movieclip, but ahhhhhhh the beauty of as3!

The first .parent is my loader and the second call to .parent is actually the SWF’s root timeline. The master displayObject is always a movieclip and isn’t a sealed class. If you’re using Sprites…you’re gonna run into some trouble. Maybe you could typecast to work around it but I’m giving no promises because i haven’t tried it.

Moral of the Story: ok, so you at least always need 2 calls to parents. (.parent.parent) one parent is the loader you used and the other is the main timeline.

OHHHHH, so you’re loading your external SWF into a movieClip are ya? In that case, just add one more .parent.

Hope that helps!

Note: The “mother ship”, master timeline, _root, etc is ALWAYS A MOVIECLIP.

51 thoughts on “AS2 vs. AS3: _root or .parent- Communicating Between SWFs

  1. Giro says:

    nothing to say aout it… just… I LOVE U!!!!! from 6 o’clock am till now i was looking for this!!!

    THANKS!!!!

  2. I want this to work so bad but so far I’ve got nothing. Here’s my code. It is producing the following Error: ArgumentError: Error #1063: Argument count mismatch on Different_Movies_fla::MainTimeline/works(). Expected 1, got 0.
    at Popup_fla::MainTimeline/closeWindow()

    HELP!

    //Master Movie
    var loader:Loader = new Loader();
    loader.load(new URLRequest(“Popup.swf”));

    picture_btn.addEventListener(MouseEvent.CLICK, showPicture);

    function showPicture(event:MouseEvent):void
    {
    addChild(loader);
    }

    function works(event:MouseEvent):void
    {
    trace(“It’s Working”);
    };

    //External Movie
    close_btn.addEventListener(MouseEvent.CLICK, closeWindow);

    function closeWindow(event:MouseEvent):void
    {
    MovieClip(this.parent.parent).works();
    };

    //What am I doing wrong?

  3. lookatme0128 says:

    function works(event:MouseEvent):void
    {
    trace(”It’s Working”);
    };

    the function above is not a MouseEvent function. closeWindow is MouseEvent function because the mouse calls it directly. any functions you call from closeWindow are no longer MouseEvents. The works function is expecting a MouseEvent, which is an arguement, and it got 0 arguements while expecting one which explains your error. SO….your code should look like:

    function works():void
    {
    trace(”It’s Working”);
    }

  4. @lookatme0128: That did the trick but doesn’t it defeat the purpose of calling a function in the “Master” .swf if you have to duplicate it.

    Take my example, imagine if works(event:MouseEvent); already performed some great function, your solution suggests that I have to duplicate that function and give it another name like: workscall();

    If I have to do that then i’m not really calling a function that I already wrote, I’m having to create then call a new one.

  5. lookatme0128 says:

    I’m sorry but i don’t understand what you’re asking.

    You’re correct, duplicating any function is unnecessary and wasteful, so don’t duplicate it?

    Maybe you can reword that?

    If i do get it, and you’re trying to make a function hat a MouseEvent function calls and just a normal function that isn’t an event, then make one function and nest it inside those 2?

  6. thanks so much! i have been trying to make my ext swf communicate with my main swf for ages, and your explanation is clear, simple, and best of all, it works!

  7. Flash Php says:

    That’s cool … But how do we achieve the opposite ?

    I’m trying to access FROM MY MASTER MOVIE a variable and the timeline of an external loaded swf (loaded in a movieclip located in my master movie)…

  8. Mauro says:

    Hey man god bless you!!! im going fucking crazy searching this migration issue in all adobe manuals and other stupid books and nothing but now, with my eyes in red, can resolve the problem, maybe this example can help someone…

    in my case i have this:

    root timeline
    |
    ->layoutMc
    |
    -> canillaMc
    |
    -> waterMc
    ok fine
    in the “canillaMc” i have a function “AniClose()”, and for invoke this from “waterMc” timeline i use:

    MovieClip(this.parent).canillaMc.AniClose();

    NOTE what the “canillaMc” reference is outside Brackets “( )”

    thanks man!!! realy thanks and sorry for my english

    best regards From Argentina Mauro

  9. Great post! Lets say that you have a master swf. that you load another .swf into via addChild and the URLRequest ect.. If I have a button in the loaded .swf and wanted to communicate back to the parent .swf I would do (and have done with great success) what you described above (MovieClip(this.parent).stop():). My question is….what if I want to do the opposite. What if you have a button on the master .swf and want to communicate up to the child swf? I basically just want to stop the loaded .swf from playing with a button on the main timeline. (MovieClip(??????).stop) I’ve tried everything I can think of and nothing seem to work? Any ideas?

  10. delta says:

    What about climbing down the hierarchy tree? If I have a loaded swf into my parent swf…how can communicate and send signals into the loaded swf from the parent swf?

  11. Hey,

    @theflashfactory, @delta, @Alex Wilson:

    I wanted to do the same, access some variable or function in the loaded swf.
    You have access to the main timeline of the loaded swf via the event-object (e.target.content):

    var urlToLoad:String = “external_swf.swf”;

    var swfLoader:Loader = new Loader();

    var swfRequest:URLRequest = new URLRequest(urlToLoad);

    swfLoader.load(swfRequest);

    this.addChild(swfLoader);

    swfLoader.contentLoaderInfo.addEventListener(Event.INIT, accessSWF);

    function accessSWF(e:Event):void
    {
    trace(“fully loaded:”+e.target.content.nameOfVariable)
    }

  12. Matt says:

    I still cannot get this to work…
    Im not sure, but I think the word “this” is whats kicking the error…
    When I try to run the function it tells me that A term is undefined and has no properties…
    Also, when I run the SWFLoad function (Which is a preloader for each page) i need to be able to tell it where to load the page into…
    Which I have setup as an empty movieclip on the main timeline in the parent swf (Displayed below as mc_content).
    When I insert that instance name, it kicks an error as being invald… how can I then reference that clip to tell it where to place the new movie??

    //Declares mc_img as a Button
    mc_img.buttonMode = true;
    //Adds listener to main image to allow for a click
    mc_img.addEventListener(MouseEvent.CLICK, redirect);
    //Gets the URL from the image clicked and loads that URL
    var url_request = url_arr[num];
    function redirect(e:MouseEvent):void {
    trace(“HI”);
    trace(url_request);
    MovieClip(this.parent.parent).SWFLoad(url_request, mc_content)
    }

  13. Jesus says:

    the first time i looked at this i was very very skeptical it was going to work, for my surprise it did, thank you very much, i spent a couple hours trying to find an answer and i got it!

    thanks again!

  14. THANKYOU! You wouldn’t believe how long I’ve been looking for this solution. I had this.parent.parent, but was missing the MovieClip()… DOH!

  15. Falling Awake says:

    I never comment on these type of sites, but this time I have to say THANK YOU. Literally I’ve spent weeks trying different methods and ways to achieve this, and this code here just solved my problem. Thank you once again.

  16. clairde says:

    GREAT!! i’ve been dealing with this problem for a few weeks, and your clear tutorial solved it in just seconds!
    Salute!
    thanks!

  17. I’m having a problem accessing a stylesheet, _loadedStyles, loaded into master from a loaded swf, home. _loadedStyles exists on master’s main timeline and is just waiting for some loaded swf to call it. I have tried

    myTextField:TextField = parent.parent.parent.parent._loadedStyles;
    and
    myTextField:TextField = Sprite(parent.parent.parent.parent.parent)._loadedStyles;

    without success. No error message. The home.swf just doesn’t load.

    Both master and home extend Sprite. In your article you mention that “If you’re using Sprites…you’re gonna run into some trouble.” I don’t understand what kind of trouble and why does it make a difference? What should I do?

    Here’s an outline of the display tree:

    root 1 [object master]
    master [object Sprite]
    .
    .
    .
    content [object Sprite]
    instance 39 [object Home] (main timeline of home)
    background [object Sprite]
    page [object Sprite]
    myTextField [object TextField]

    This post is fairly old. Hopefully you are still monitoring it.

    Thanks

  18. Dude!
    F***ing awesome this small piece off code! It just made my day!! I was struggling for over a week now to find a way to communicate from a loaded swf to the master-swf. Thanks to you I managed to do it. Thanks a lot man!

  19. Chuck says:

    You say, “If you’re using Sprites…you’re gonna run into some trouble. Maybe you could typecast to work around it but I’m giving no promises because i haven’t tried it.” My SWF is loaded with the UILoader on the main (root) timeline. When I try your solution (for which I thank you greatly!) this is what I’m getting.
    TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Sprite@2be78801 to flash.display.MovieClip.

    I don’t know how to “typecast,” if that will fix the problem.

  20. whiteRoom says:

    Soooooo you wan’t to grab a root var from a swf loaded 2 deep?
    index.swf/child.swf/child-child.swf some how this equals 7 parents.
    trace(MovieClip(this.parent.parent.parent.parent.parent.parent.parent).variableName + ” index var name”);
    WORKS! I don’t know why the lucky numb 7, but hay whatever works eh.
    Thanks!

  21. jun.p says:

    hello,

    I have some issues which I believe related to your solutions.

    I have loaded two swfs on the stage. Now I when I am inside of first loaded swf I want to access any method/variable in the loaded second swf. How could I do this?

    Please help…. extreme headache now.

  22. I have my function in the master fla and then I put MovieClip(this.parent.parent).turnOffPreloader(); to call it in my loaded swf and I get this error.

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at index_fla::MainTimeline/frame1()

    I used:
    if (this.parent.parent==null) {
    trace(this.parent.parent);
    } else {
    MovieClip(this.parent.parent).turnOffPreloader();
    }

    to get it too compile and now I get:

    TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Stage@2601cb51 to flash.display.MovieClip.
    at index_fla::MainTimeline/checkLoaded()

    What am I doing wrong? 😦

  23. Wow thats AS3 newib can understand. Now im finding little difficult in interacting with external swf from main movie,
    parent have function (e:Event):void{}
    child have button
    parent function loads the external swf on parent mc. when child button get hit should replace with new swf on parent mc.

  24. I also have one issue getting errors in output, even it works, but in IE7 or IE8 the functions are prompted. I figured it out as flash version 10.0.0 its getting error prompts and 10.1.0 it is not showing. how to fix please help me…

Leave a comment