SWF 嵌套中的 addEventListener
我当前有一个 .swf 文件嵌套在另一个 .swf 文件中。
在父 SWF 文件中,我使用 UILoader 加载其他 .swf 文件。
uiLoader.source = "数据/child.swf";
-
在子 SWF 文件中,我有
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyPressedDown);
当我单独运行它时,它运行得很好;但是当我通过parent.swf stage.addEvent运行child.swf时...给我一个空引用异常。
阶段是导致问题的原因吗?如果是,有没有办法解决这个问题?
I currently have a .swf file that is nested into another .swf file.
In the parent SWF file I use a UILoader to load the other .swf file.
uiLoader.source = "data/child.swf";
-
In the child SWF file I have
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyPressedDown);
and when I run it on it's own, it works perfectly; but when I run child.swf through parent.swf stage.addEvent... give me a null reference exception.
Is the stage part of what is causing the issue?, and if so, is there a way to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,这是一个好问题,我花了一些时间才弄清楚。
基本上Flash会做这个奇怪的事情(也许是一个错误?),但在实际初始化对象之前运行函数。这种情况也发生在舞台上初始化影片剪辑时:
var mc:something = new Something();
addChild(something)
现在在 some 中。就好像您在初始化函数中引用了某个阶段一样,它将给出 null。 (参考:http://www.emanueleferonato.com/2009/12 /03/understand-added_to_stage-event/)
所以基本上解决了同样的问题并将其扩展到 urlLoader,它在实际构建其层次结构阶段之前运行您的代码 -> 以上
现在,为了解决这个问题,请在您的孩子 swf 中执行以下操作:
是我的代码,您可以废弃其中的大部分内容,但主要要注意的是:
addEventListener(Event.ADDED_TO_STAGE, init);
在对象初始化后执行。
Ok this is a good question, took me a little while to figure it out.
Basically Flash does this wierd thing (maybe a bug?) but runs the functions before actually initializing the objects. This happens with initializing movieclips with just on stage as well:
var mc:something = new something();
addChild(something)
now in something.as if you had a reference to a stage in the initialize function it would give null. (reference: http://www.emanueleferonato.com/2009/12/03/understand-added_to_stage-event/)
So basically taking that same problem and extending it to urlLoader it's running your code before actually building its hierarchy stage -> movie clips
now in order to solve this problem do something like this in your child swf:
The above is my code, you can scrap most of it, but the main thing to note is that:
addEventListener(Event.ADDED_TO_STAGE, init);
executes after your objects are initialized.
我不完全确定,但可能是因为带有事件侦听器的 MovieClip 嵌套在另一个 MovieClip 中,所以它无法访问“舞台”对象。
要尝试的一件事是从舞台中删除 eventListener,因此它看起来就像这样:
这可能有效。或者,您可以将事件代码保留在父级 MovieClip 中。希望这些事情可能有所帮助。
德布
I'm not entirely sure, but it could be that because the MovieClip with the event listener is nested inside another MovieClip, it doesn't have access to the 'stage' Object.
One thing to try, would be to remove the eventListener from stage, so it simply looks like this:
That might work. Alternatively, you could just keep the event code in the parent MovieClip. Hope these things might help.
Debu