访问加载的 .swf 文件中的函数?
有没有办法在加载的 SWF 文件中调用函数?
基本上,我有一个 .swf 文件 (A),它加载另一个 .swf 文件 (B)...我只想将 B 视为添加到我的类 .swf“A”中的任何其他实例
...使用 .swf 文件类的名称重铸“Loader”:
已加载 .swf 类:
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);
}
}
}
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两种情况,即
首先,在这两种情况下,您必须确保加载的 swf(B) 已被加载使用
Event.COMPLETE
加载并添加到 Loader swf(A)。这样两个swf之间的通信就可以了。加载的 swf 就像任何其他子文件一样。以下是情况 2 的示例代码
embedSWF.function_OF_B()
语句将调用子 swf B 的函数,即function_OF_B()
There are two cases i.e
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
embedSWF.function_OF_B()
statement will call the function of child swf B i.efunction_OF_B()
在 Adobe Flex 中,您可以使用 flash.display.Loader 类加载另一个 SWF,并向其添加事件侦听器。
此示例取自 Adobe 文档:
由于 contentLoaderInfo 是 EventDispatcher 的子类,因此您还可以将事件分派到加载的 SWF。
这基本上就像从 SWF 中调用函数一样,只是稍微复杂一些。
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:
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.