AS2:如何在通过 MovieClipLoader 加载的 SWF/MovieClip 上使用 addEventListener

发布于 2024-07-29 21:01:39 字数 842 浏览 3 评论 0原文

我正在尝试加载本地 SWF,然后捕获一些被触发的事件,但我不明白为什么这不起作用。

这是代码

Parent.swf

_mcl = new MovieClipLoader();
_mcl.addListener(this);
_mcl.loadClip("Child.swf", rotator_mc);

function onLoadComplete(clip) {
    clip.addEventListener("initAsLoaded", function() {trace("childLoaded loaded 1")});
}

function onLoadInit(clip) {
    clip.addEventListener("initAsLoaded", function() {trace("childLoaded loaded 2")});
}

Child.swf

import mx.events.EventDispatcher;

function dispatchEvent() {};
function addEventListener() {};
function removeEventListener() {};

EventDispatcher.initialize(this);

trace("Child dispatching: childLoaded");
dispatchEvent({type:"childLoaded", target: this});

现在我希望这能起作用,并且 Parent 会在跟踪中包含“childLoaded catch 2”,但事实并非如此。

有什么办法可以实现我想要做的事情吗?

I'm trying to load a local SWF then catch a few events that are fired but I can't see why this isn't working.

Here's the code

Parent.swf

_mcl = new MovieClipLoader();
_mcl.addListener(this);
_mcl.loadClip("Child.swf", rotator_mc);

function onLoadComplete(clip) {
    clip.addEventListener("initAsLoaded", function() {trace("childLoaded loaded 1")});
}

function onLoadInit(clip) {
    clip.addEventListener("initAsLoaded", function() {trace("childLoaded loaded 2")});
}

Child.swf

import mx.events.EventDispatcher;

function dispatchEvent() {};
function addEventListener() {};
function removeEventListener() {};

EventDispatcher.initialize(this);

trace("Child dispatching: childLoaded");
dispatchEvent({type:"childLoaded", target: this});

Now I was hoping that this would work, and the Parent would have "childLoaded caught 2" in the trace, but it doesn't.

Is there any way to achieve what I'm trying to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦冥 2024-08-05 21:01:39

我注意到的第一件事是,在父级中,您列出了一个名为 initAsLoaded 的事件,但在子级中,您正在调度一个名为 childLoaded 的事件。 监听永远不会发送的事件可能不是一个好主意。

然而,这不是真正的问题,因为修复事件并不能解决问题。 主要问题是 MovieClipLoader 中的 onLoadInit 事件是在执行 child.swf 第一帧的代码之后调度的。 这意味着在 child.swf 中,您将在没有人监听的第一帧上调度一个事件,因为尚未调用 onLoadInit

要解决此问题,您只能从第二帧及以后开始调度 child.swf 中的事件。

父母第一帧:

var rotator_mc = _root.createEmptyMovieClip("rotator_mc", _root.getNextHighestDepth() );

_mcl = new MovieClipLoader();
_mcl.addListener(this);
_mcl.loadClip("child.swf", rotator_mc);

function onLoadComplete(clip) {
    trace("onLoadComplete " + clip + ", " + clip.addEventListener );
    clip.addEventListener("initAsLoaded", function() {trace("childLoaded loaded 1")});
}

function onLoadInit(clip) {
    trace("onLoadInit " + clip + ", " + clip.addEventListener );
    clip.addEventListener("initAsLoaded", function() {trace("childLoaded loaded 2")});
}

这是我对你父母第一帧的版本。 我必须创建一个空的影片剪辑作为容器,并向事件处理程序添加一些跟踪。

儿童第一帧:

import mx.events.EventDispatcher;

EventDispatcher.initialize( this );

儿童第二帧:

stop();

this.dispatchEvent({type:"initAsLoaded", target: this});

trace("Child dispatching: childLoaded");

希望这有帮助...

The first thing I noticed was the fact that in the parent you are listing for an event called initAsLoaded but in the child you're dispatching an event called childLoaded. Listening for an event that will never be dispatched might not be a good idea.

However, this is not the actual issue because fixing the events doesn't solve the problem. The main problem is that the onLoadInit event from the MovieClipLoader is dispatched after the code of the first frame of child.swf has been executed. This means that in child.swf you're dispatching an event on the first frame that no one is listening too because the onLoadInit hasn't been called yet.

To fix this you can only start dispatching events in the child.swf from the second frame and onwards.

Parent first frame:

var rotator_mc = _root.createEmptyMovieClip("rotator_mc", _root.getNextHighestDepth() );

_mcl = new MovieClipLoader();
_mcl.addListener(this);
_mcl.loadClip("child.swf", rotator_mc);

function onLoadComplete(clip) {
    trace("onLoadComplete " + clip + ", " + clip.addEventListener );
    clip.addEventListener("initAsLoaded", function() {trace("childLoaded loaded 1")});
}

function onLoadInit(clip) {
    trace("onLoadInit " + clip + ", " + clip.addEventListener );
    clip.addEventListener("initAsLoaded", function() {trace("childLoaded loaded 2")});
}

This is my version of your parent's first frame. I had to create an empty movie clip to serve as a container and I added some traces to the event handlers.

Child first frame:

import mx.events.EventDispatcher;

EventDispatcher.initialize( this );

Child second frame:

stop();

this.dispatchEvent({type:"initAsLoaded", target: this});

trace("Child dispatching: childLoaded");

Hope this helps...

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