Access to the function inside the downloaded .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 process B as if it were some other instance added to my .swf class " A "...


You need to redo "Loader" with the name of your class .swf file:

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

}

+4
source share
2 answers

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

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

+4
source

There are two cases: i

  • Child swf (B) calls the function Parent swf (A) or
  • Parent swf (A) calls the loaded swf (B) function

First, in both cases, you must ensure that the loaded swf (B) has been loaded and added to the swf (A) bootloader using Event.COMPLETE . Then a connection between the two swfs is possible. Loaded swf is like any other child.

Here is a 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(); } 
Operator

embedSWF.function_OF_B() will call the function of the child swf B ie function_OF_B()

+7
source

All Articles