ActionScript 3.0: how to call a function in the main swf from the loaded swf
thanks to flashforum.dk and Yogesh Puri I finally found a way to solve this problem.
actually I got 2 solutions.
the first one is by using an eventlistener.
in your main .swf you load the other .swf called green.swf.
then you put up an eventListener await the custom event: BlackClick
when the event is triggered, you trace it.
here’s the code for the main .swf:
import flash.display.Sprite;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
public class test extends Sprite {
private var _greenloader:Loader;
public function test() {
trace("inside AS");
_greenloader = new Loader();
addChild(_greenloader);
_greenloader.contentLoaderInfo.addEventListener(Event.INIT, handleInit);
_greenloader.load( new URLRequest("green.swf"));
}
private function handleInit(e:Event):void {
_greenloader.content.addEventListener("BlackClick", clickFromChild);
}
public function clickFromChild(e:Event):void {
trace("clicked: "+e);
}
}
}
there’s nothing really tricky in the code for the green.swf.
you just set up a movieclip, and when this is clicked you trigger the custom event BlackClick:
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
public class greentest extends Sprite {
public function greentest() {
black.buttonMode = true; //black is a movieclip on stage
black.addEventListener(MouseEvent.CLICK, dispatchTheEvent);
}
private function dispatchTheEvent(e:Event):void {
trace("function dispatchTheEvent");
dispatchEvent(new Event("BlackClick"));
}
}
}
—–
heres the second version of calling a function in the main .swf from the loaded .swf.
same procedure, almost; you load the green.swf into your main .swf.
this time, instead of listening for a custom Event, you just setup your function to be called from the loaded .swf.
this function is the docFunction and it simply traces out its name.
the code for the main .swf is:
import flash.display.Sprite;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
public class test extends Sprite {
private var _greenloader:Loader;
public function test() {
trace("inside AS");
_greenloader = new Loader();
addChild(_greenloader);
_greenloader.load( new URLRequest("green.swf"));
}
public function docFunction() {
trace("function: docFunction");
}
}
}
to be able to call the function in the main.swf without knowing it when compiling the green.swf (and avoiding compiler-errors!) you need to typecast the parent of your green.swf as an Object which is a dynamic class, and then call the function.
like this:
(root.loaderInfo.loader.root as Object).docFunction();
and so; here’s the full code for the green.swf:
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class greentest extends Sprite {
public function greentest() {
black.buttonMode = true;//black er et movieclip på stage
black.addEventListener(MouseEvent.CLICK, clickFromChild);
}
public function clickFromChild(evt:MouseEvent){
(root.loaderInfo.loader.root as Object).docFunction();
}
}
}
—–
hope you find this as useful as I did.
:O)