Monday, January 19, 2009

AS3 - How to call a function in loaded external swf

At the parent code:

package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;

public class ExampleA extends Sprite
{
private var loader:Loader = new Loader();

public function ExampleA()
{
loader.contentLoaderInfo.addEventListener( Event.INIT, onLoaderInit );
loader.load( new URLRequest( "ExampleB.swf" ) );
}

private function onLoaderInit( e:Event ):void
{
Object( loader.content ).init( "hello" );
}
}
}


at the child code:
package
{
import flash.display.Sprite;

public class ExampleB extends Sprite
{
public function ExampleB()
{}

public function init( param:String ):void
{
trace( param ); // hello
}
}
}