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.
I was searching many hours on the web for this and it worked for me now!
THANK YOU VERY MUCH!
THANKS!
Thanks Brother, perfect!
Using AS2…
Sweet and nice…
Was going crazy by changing all sort of stuff on my external swf. now in just a few line, everything perfectly.
Thanks again for that info.
nothing to say aout it… just… I LOVE U!!!!! from 6 o’clock am till now i was looking for this!!!
THANKS!!!!
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?
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”);
}
@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.
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?
Forgive me, I never even considered nesting functions before! You save my life today.
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!
apparently you were the only one in the world who knew this. THANK YOU!!
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)…
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
Huge thanks! I had it as a sprite and that’s why it wouldn’t load.
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?
Thank you!
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?
that’s a very helpful trick! thank you!
anyone found out how to communicate to the loaded swf yet? Ive looked evrywhere 😦
TFF
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)
}
Thanks so much. Saved me a lot of time!
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)
}
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!
Oh man…. you saved me so many hours with that hint…
I thank you with all my heart. 😀
Just to join the chorus “you save my life today”
Thanks!
Thanks so much, I was stuck in this precise problem :3
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!
thank you
Thanks for this man
I was searching many DAY on the web for this and it worked for me now!
THANK YOU VERY MUCH!
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.
Thanks a lot, found you through google, and now I love you 🙂
GREAT!! i’ve been dealing with this problem for a few weeks, and your clear tutorial solved it in just seconds!
Salute!
thanks!
how would one best communicate
from a function in the loaded swf
to a movieclip on the parent timeline.
I’ll like to change its attribute (visible)
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
Terrific! You’ve saved my life. Will you marry me?
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!
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.
thank you very much it was very strng for me but now I can use AS3 very easy with this tip.
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!
thanx very much…that parent.parent thing was the real culprit which i was missing
Man Thanks. You know the story. I searched and searched and finally found this page. Now I can finish the job get paid and go out and get laid
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.
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? 😦
Can someone post a file? This does not work for me. 😦
Thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
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.
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…
here is the link to therad
http://www.sitepoint.com/forums/flash-actionscript-150/flash-cs5-as3-errors-ie-7-ie-8-a-756163.html
You wrote that many many many years ago, but today you saved my life. thank you