访问加载的 .swf 文件中的函数?

发布于 2024-09-24 02:40:34 字数 1262 浏览 3 评论 0原文

有没有办法在加载的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乞讨 2024-10-01 02:40:34

有两种情况,即

  1. 子 swf(B) 调用父 swf(A) 的函数或
  2. 父 swf(A) 调用加载的 swf(B) 的函数。

首先,在这两种情况下,您必须确保加载的 swf(B) 已被加载使用 Event.COMPLETE 加载并添加到 Loader swf(A)。这样两个swf之间的通信就可以了。加载的 swf 就像任何其他子文件一样。

以下是情况 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() 语句将调用子 swf B 的函数,即 function_OF_B()

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

独行侠 2024-10-01 02:40:34

在 Adob​​e Flex 中,您可以使用 flash.display.Loader 类加载另一个 SWF,并向其添加事件侦听器。

此示例取自 Adob​​e 文档:

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 中调用函数一样,只是稍微复杂一些。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文