Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access a function inside a loaded .swf file?

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

}

like image 948
redconservatory Avatar asked Dec 16 '22 21:12

redconservatory


2 Answers

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

like image 62
Muhammad Irfan Avatar answered Dec 19 '22 11:12

Muhammad Irfan


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.

like image 28
user434565 Avatar answered Dec 19 '22 11:12

user434565