views:

106

answers:

2

Is there a way to call a function inside a loaded SWF file?

Basically, I have a .swf file (A) that loads another .swf file (B)...I would just like to treat B as if it was any other instance added to my class .swf "A"...


Have to recast "Loader" with the name of your .swf file class:

Loaded .swf class:

package src {
import flash.display.MovieClip;

public class LoadedSWF extends MovieClip     {
    public function LoadedSWF() {
    }

    public function helloWorld():void
    {
        trace("hello world from loaded swf!");
    }
}
}

Main class:

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

public class Main extends MovieClip {

    private var loader:Loader;

    public function Main() {
        loadSWF("LoadedSWF.swf")
    }

    private function loadSWF(url:String):void {
        var urlRequest:URLRequest = new URLRequest(url);
        loader = new Loader();
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded, false, 0, true);
        loader.load(urlRequest);
        addChild(loader);

    }

    private function onLoaded(e:Event):void {
        var target:LoadedSWF = e.currentTarget.loader.content as LoadedSWF;
        trace(target);
        target.helloWorld();

        addChild(target);
    }
}

}

+1  A: 

In Adobe Flex, you can use the flash.display.Loader class to load another SWF, and add an event listener to it.

This example is taken from the Adobe Documentation:

var url:String = "http://www.helpexamples.com/flash/images/image2.jpg";
var urlRequest:URLRequest = new URLRequest(url);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
loader.load(urlRequest);
addChild(loader);

function loader_complete(evt:Event):void {
    var target_mc:Loader = evt.currentTarget.loader as Loader;
    target_mc.x = (stage.stageWidth - target_mc.width) / 2;
    target_mc.y = (stage.stageHeight - target_mc.height) / 2;
}

Because contentLoaderInfo is a subclass of EventDispatcher, you can also dispatch events to the loaded SWF. This is basically like calling a function from the SWF, just a little more complicated.

Just for clarity, this is not a Flex specific solution as suggested. In fact, none of the code here is specific to Flex and is straight ActionScript 3 that will compile without the Flex libraries imported.
Tegeril
This helps a lot, thanks. I took the example with target_mc and recast it with my .swf class name and it worked!
redconservatory
Edited version above...
redconservatory
+1  A: 

There are two cases i.e

1--child swf(B) calls function of Parent swf(A) OR 2--Parent swf(A) calls function of loaded swf(B)

Firstly, in both cases, You must make sure that loaded swf(B) has been loaded and added to Loader swf(A) using Event.COMPLETE. Then communication between two swf is possible. The Loaded swf is just like any other child

Here is the sample code for case-2

var mLoader:Loader = new Loader(); mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler); mLoader.load(new URLRequest("B.swf"));

public function onCompleteHandler(evt:Event) { var embedSWF:MovieClip = MovieClip(evt.target.content); addChild(embedSWF); embedSWF.function_OF_B();

}

embedSWF.function_OF_B() statement will call the function of child swf B i.e function_OF_B()

Muhammad Irfan