AS2:如何在通过 MovieClipLoader 加载的 SWF/MovieClip 上使用 addEventListener
我正在尝试加载本地 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我注意到的第一件事是,在父级中,您列出了一个名为
initAsLoaded
的事件,但在子级中,您正在调度一个名为childLoaded
的事件。 监听永远不会发送的事件可能不是一个好主意。然而,这不是真正的问题,因为修复事件并不能解决问题。 主要问题是
MovieClipLoader
中的onLoadInit
事件是在执行 child.swf 第一帧的代码之后调度的。 这意味着在 child.swf 中,您将在没有人监听的第一帧上调度一个事件,因为尚未调用onLoadInit
。要解决此问题,您只能从第二帧及以后开始调度 child.swf 中的事件。
父母第一帧:
这是我对你父母第一帧的版本。 我必须创建一个空的影片剪辑作为容器,并向事件处理程序添加一些跟踪。
儿童第一帧:
儿童第二帧:
希望这有帮助...
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 calledchildLoaded
. 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 theMovieClipLoader
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 theonLoadInit
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:
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:
Child second frame:
Hope this helps...