Flexcomps’s Weblog

AS3 – calling myFunction() on main timeline from loaded SWF

Posted by: Yogesh Puri on: September 1, 2008

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

18 Responses to "AS3 – calling myFunction() on main timeline from loaded SWF"

Hi I like this site im glad you updated it . I like the new options.

Thanks man.. I couldnt for the life of me figure out a way to do this.

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?

Simple, and perfect explanation!!
Thankyou very much! []’s

If you are trying to call a function inside parent loader you should do this:

(root.loaderInfo.loader.root as Object).myFunction();

THANKS YOU SAVED MY ASS!!!!!!!

Thank you very much, i was desperately searching for the same, and atlast got it working, Thanks again!

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()

I didn’t got exactly your problem. You can mail me your files at yogeshpuri at gmail.com or post some more details.

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

[...] to flashforum.dk and Yogesh Puri I finally found a way to solve this [...]

hi Yogesh Puri.

thanks, this post came in very, very handy.
felisan

I love you, thanks for this been annoying me for ages – seems so simple lookin at it now though

Thanks! Your solution saved my ass

Wow .. thanx a lot..
Little script can make such a big difference.

Thanks man…
You saved my life…………..

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);

Leave a Reply

 

September 2008
M T W T F S S
« Aug   Oct »
1234567
891011121314
15161718192021
22232425262728
2930  

Blog Stats

  • 131,528 hits