访问函数内加载SWF文件? [英] Access a function inside a loaded .swf file?
问题描述
有没有办法来调用一个函数加载的SWF文件中?
Is there a way to call a function inside a loaded SWF file?
基本上,我有一个.swf文件(一)加载另一个SWF文件(B)......我只想把b以,如果它加入到我的课瑞士法郎的A的任何其他实例。 ..
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"...
有重铸装载机与您.swf文件类的名称:
Have to recast "Loader" with the name of your .swf file 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!");
}
}
}
主要类:
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);
}
}
}
推荐答案
在Adobe Flex的,你可以使用flash.display.Loader类加载另一个SWF和事件监听器添加到它。
In Adobe Flex, you can use the flash.display.Loader class to load another SWF, and add an event listener to it.
这个例子是采取从Adobe的文档:
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;
}
由于的contentLoaderInfo是的EventDispatcher的子类,还可以调度事件,以便加载的SWF。 这基本上就像从SWF调用一个函数,只是稍微复杂一点。
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.
这篇关于访问函数内加载SWF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!