The post is about loading an external swf in a main swf file with some function on main timeline and calling those functions from loaded swf file.
Now the problem is when you try to compile the external swf file with any reference to the main timeline using parent.parent or loader.parent etc. it gives error in compilation time due to the behaviour of concrete classes i.e. MovieClip, Sprite, DisplayObject so how to make a call on parent timeline where there is no direct reference. Following is one of the approach i have found quickly. It is a tricky way to do this. What you need to do is typecast the parent of loader as an Object which is a dynamic class and then call the method which gets compiled without any error. Following is the statement for the same
if(this.parent.parent != null){
var parentObj:Object = this.parent.parent as Object;
parentObj.traceMe()
}
You can download the sample files from here . Kindly let me know if you found any other approach
StreetM on September 9, 2008 at 12:01 am said:
Hi I like this site im glad you updated it . I like the new options.
Dave on October 10, 2008 at 9:01 pm said:
Thanks man.. I couldnt for the life of me figure out a way to do this.
michael on November 11, 2008 at 6:09 am said:
wooooow,
i’ve been waging hopeless war against as3, looking everywhere for help and calling to my deepest intuition with no success, until 5 am when i ran into this little little post.
thank you
what does the “as” command mean?
Eder Lima on November 13, 2008 at 10:04 pm said:
Simple, and perfect explanation!!
Thankyou very much! []‘s
talcho chat ;) on December 7, 2008 at 6:24 pm said:
If you are trying to call a function inside parent loader you should do this:
(root.loaderInfo.loader.root as Object).myFunction();
yumgsta on July 19, 2009 at 3:24 pm said:
THANKS YOU SAVED MY ASS!!!!!!!
THEPTSMAN on January 19, 2010 at 9:13 pm said:
(root.loaderInfo.loader.root as Object).addChild(object1);
Allowed me to addChild to my main timeline object1.swf
from inside of a child… WORKED GREAT!!! SO
Maintimeline > teaser1 > object1
since teaser1 was loaded into the maintime line I wanted
object1 to also be loaded into the main time line to overlap
everything on the maintimeline! This was the ticket! thank you,
talcho!
zach wilson on March 10, 2012 at 10:06 pm said:
you are a god
virtualvinay on December 8, 2008 at 6:52 am said:
Thank you very much, i was desperately searching for the same, and atlast got it working, Thanks again!
virtualvinay on December 19, 2008 at 6:21 am said:
Hi,
I tried it using timeline scripting it works, but when i am using it with an external .as file it gives an error
TypeError: Error #1006: callTrace is not a function.
at anim_fla::MainTimeline/anim_fla::frame50()
Yogesh Puri on December 19, 2008 at 6:29 am said:
I didn’t got exactly your problem. You can mail me your files at yogeshpuri at gmail.com or post some more details.
Danil on January 29, 2009 at 3:51 pm said:
hello, i have main.swf with loaded 1.swf in it and loaded 1.swf in 1.swf
main.swf>1.swf>1.swf
now, how can i access function in main.swf from second 1.swf???
ty
felisan on February 23, 2009 at 8:38 am said:
hi Yogesh Puri.
thanks, this post came in very, very handy.
felisan
Mark machine on March 5, 2009 at 1:27 am said:
I love you, thanks for this been annoying me for ages – seems so simple lookin at it now though
SCB on March 11, 2009 at 5:19 pm said:
Thanks! Your solution saved my ass
Jess on June 29, 2009 at 9:52 am said:
Wow .. thanx a lot..
Little script can make such a big difference.
Inder on July 9, 2009 at 3:45 am said:
Thanks man…
You saved my life…………..
fair on August 26, 2009 at 11:21 pm said:
Hi.
when i add the loader in a mc on the stage i get this error.
TypeError: Error #1006: traceMe is not a function.
var myMc:MovieClip = new MovieClip();
stage.addChild(myMc);
myMc.addChild(loader);
Leroy on November 10, 2009 at 11:00 pm said:
External movieclip’s calling myFunction() on main timeline from loaded SWF via libraryMC.
When you want to load a movie clip on to the stage from library by linkage and call the demo Movieclips functions from the externalMovie’s moviclip timeline, accessing it is different:
//Load movieclip from library to externalMovie.swf stage
var MC:libraryMC = new libraryMC();
MC.x = 15;
MC.y = 10;
this.addChild(MC);
//Action on the first from libraryMC
function onLoaded(){
trace(“I am inner content”);
trace(root+” ” +this.parent+”I am “+this);
if(this.root.loaderInfo.loader.root != null){
var parentObj:Object = this.root.loaderInfo.loader.root as Object;
parentObj.traceMe()
}
}
***this.root.loaderInfo.loader.root***
instead of “this.parent.parent”
Steve on November 12, 2009 at 11:31 am said:
Hi,
as everything in life: when you know how to do it`s so simple…
Thanks a lot for the snippets…
In my project I found these two solutions – both are working:
1) (parent.root as Object).my_function();
2) if (this.parent.parent != null) {
var parentObj:Object = this.parent as Object;
parentObj.my_function();
}
Perhaps it helps someone sometimes…
Steve
pablo on November 12, 2009 at 5:29 pm said:
Thank you very much. Again, I´m afraid. 2.28 am. I can go to sleep now.
Glenn on December 10, 2009 at 3:30 am said:
Thanks for the files, i was looking for this, i have a problem with child accessing main .swf file. AS3 is kinda confusing.
Sarah Snyder on December 14, 2009 at 3:44 pm said:
Thank you!
Been trying to figure this out all morning!!!!
jpauclair on December 23, 2009 at 12:55 pm said:
You should read my Sucurity Analysis on flash override security risk:
http://jpauclair.wordpress.com/2009/12/11/flash-player-security-advanced-walkthrough/
Amul Patel on December 29, 2009 at 8:37 am said:
Dude you are a lifesaver.. I was pulling my hair out trying to get this to work…
AS3 is really class based with the private/public accessors and extenders etc.. I’ve been avoiding trying to learn all that.
This helps as a crutch for now.
THANKS!!!
Robert on July 7, 2010 at 8:11 pm said:
Hey.. this all looked very good.. but none of it works for me.. i do not know why..
I will keep trying, meanwhile thanks.. and can someone ask the nice people at Adobe why something so basic is such a pain
Robert on July 7, 2010 at 9:01 pm said:
Ok.. it does work, but sadly a required api from doubleclick studio breaks it.. .. GRRR
Quarther on July 22, 2010 at 6:55 pm said:
Thanks! This is the solutions I was looking for…
Kusacubari on August 16, 2010 at 2:45 am said:
nice brain you got there lad – works a treat
Black Hair Styles on October 31, 2010 at 2:01 pm said:
Keep it up, nice post! This was the info I needed to know.
matej on January 28, 2011 at 10:12 am said:
love you
Mik on March 2, 2011 at 11:01 am said:
How to put this function working in button? I’ve tried to do this
by
function click00(event:MouseEvent):void {
onLoaded();
}
but no result
I am trying to create my first page in as3 and loading swf into main timeline by another loaded before swf
can you help me with this?
Iago C. Sallezze on May 25, 2011 at 1:55 pm said:
Parabéns, procurei varias formas, mas so consegui entender e criar o meu próprio quando li o seu arquivo.
UP!!!
Ebz on May 26, 2011 at 8:52 pm said:
Any chance you can translate this to AS 2, would greatly appreciate it…thanks
Arthur on October 2, 2011 at 6:14 pm said:
or you could just turn strict mode off for as3, thats the simpler approach.
Kalin on December 6, 2011 at 2:47 am said:
MANY THANKS TO EVERYONE!!!
THIS PAGE HELPS ME A LOT