在 Air 应用程序中加载外部 .swf 文件导致循环构造函数
我以前从未遇到过这个问题,看起来真的很奇怪。我正在构建一个嵌入并显示 .swf 文件的 Air 应用程序(直接 AS3,无 Flex)。 这是我的代码:
public class Something extends Sprite
{
private var loader:Loader;
public function Something():void
{
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
loader = new Loader();
var appDomain:ApplicationDomain = new ApplicationDomain();
var context:LoaderContext = new LoaderContext(false, appDomain);
loader.load(new URLRequest("test.swf"), context);
trace("hello");
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
}
private function loaded(e:Event):void
{
trace(loader.contentLoaderInfo.sameDomain);
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loaded);
addChild(loader.content);
}
}
这不仅没有像我想要的那样将 .swf 文件添加到显示列表中,而且似乎一遍又一遍地运行。 “hello”填充跟踪语句的输出窗口,就像它在每一帧运行一样。
该代码加载 .jpg 效果很好,因此它一定是 Flash 文件特有的问题。我认为如果存在冲突而不是这个奇怪的循环构造函数,我会得到某种安全错误。
有人知道是什么导致了这种行为吗?
更新:我终于找到了这个—— 加载外部 swf 时发生闪存崩溃(带有这次的代码示例)
所以我认为发生的情况是,因为两个 Flash 文件共享相同的应用程序域,并且它们都将“Main.as”作为主类文件,所以它一遍又一遍地运行构造函数。
不过,我仍然遇到问题,我已更新了上面的代码,以显示我尝试使用不同的应用程序域加载外部 .swf 的失败尝试。
我现在在输出窗口中得到这个:
hello
[Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference.
有什么想法吗?
I've never encountered this problem before, it seems really bizarre. I'm building an Air application (straight AS3 no flex) that embeds and displays a .swf file.
Here is my code:
public class Something extends Sprite
{
private var loader:Loader;
public function Something():void
{
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
loader = new Loader();
var appDomain:ApplicationDomain = new ApplicationDomain();
var context:LoaderContext = new LoaderContext(false, appDomain);
loader.load(new URLRequest("test.swf"), context);
trace("hello");
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
}
private function loaded(e:Event):void
{
trace(loader.contentLoaderInfo.sameDomain);
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loaded);
addChild(loader.content);
}
}
Not only does this not add the .swf file to the display list like I want it to but it seems to run over and over again. "hello" fills up the output window from the trace statement like it's running every frame.
The code loads a .jpg just fine so it must be a problem specific to flash files. I would think I would get some kind of security error if there was a conflict not this strange looping constructor.
Does anybody have any ideas what is causing this behavior?
UPDATE: I finally found this --
flash crash when loading external swf (with code example this time around)
So I think what was happening is that because both flash files shared the same application domain and they both had "Main.as" as the main class file it was running the constructor over and over again.
I'm still haveing trouble though, I've updated my code above to show my unsuccessful attempts to load the external .swf with a different application domain.
I now get this in the output window:
hello
[Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,
您可能已将此文件发布为“test.swf”并且正在加载“test.swf”,因此您正在递归地重新加载同一个文件,该文件是哪个?
Hmm,
It could be possible that you have published this file as "test.swf" and you are loading "test.swf" so you are recursively reloading the same file which is this file?
请确保您的加载器的主类与加载的 swf 的主类不同
plase make sure that the main class of your loader doesn't same with main class of your loaded swf