是否可以在预加载器启动之前加载非文档类?
public class Framework extends MovieClip
{
var _loadingSystem:LoadingSystem;
public function Framework()
{
_loadingSystem = new LoadingSystem(this);
loaderInfo.addEventListener(ProgressEvent.PROGRESS,progressHandler);
loaderInfo.addEventListener(Event.COMPLETE, completeListener);
}
...
public class LoadingSystem extends MovieClip
{
public function LoadingSystem(parent:DisplayObjectContainer)
{
parent.addChild(this);
myLogo.buttonMode = true;
myLogo.addEventListener(MouseEvent.CLICK, gotoMySite);
}
如您所见,Framework 是我的 Doc 类,它正在创建 _loadingSystem,它基本上是一个包含预加载器图形的影片剪辑。当我调试时,出现以下错误“TypeError:错误#1009:无法访问空对象引用的属性或方法。”指向 myLogo.buttonMode = true;
据我了解,这是由于 LoadingSystem 在框架中创建之前未完全加载。我有什么办法可以完成这项工作吗?我尝试为 Event.ADDED 添加监听器,但没有成功。
附加信息:3 帧 FLA,第一个是空的,有一个停止点,第二个包含 AssetHolder 影片剪辑,第三个用于应用程序。我在发布设置中设置了第二帧导出,在资源中未选中第二帧导出的所有复选框,在我更改第二帧设置导出之前,这一切都有效,只是它没有预加载文件的 50%。
public class Framework extends MovieClip
{
var _loadingSystem:LoadingSystem;
public function Framework()
{
_loadingSystem = new LoadingSystem(this);
loaderInfo.addEventListener(ProgressEvent.PROGRESS,progressHandler);
loaderInfo.addEventListener(Event.COMPLETE, completeListener);
}
...
public class LoadingSystem extends MovieClip
{
public function LoadingSystem(parent:DisplayObjectContainer)
{
parent.addChild(this);
myLogo.buttonMode = true;
myLogo.addEventListener(MouseEvent.CLICK, gotoMySite);
}
As you can see, Framework is my Doc class which is creating _loadingSystem which is basically a movieclip that contains the preloader graphics. When I debug I get the following error "TypeError: Error #1009: Cannot access a property or method of a null object reference." pointing to myLogo.buttonMode = true;
From what I understand this is due to LoadingSystem not being fully loaded before being created in Framework. Is there any way for me to make this work? I have tried adding listeners for Event.ADDED but it didn't work.
Additional info: 3-frame FLA, first empty with a stop, second holding an AssetHolder movieclip, third for the application. I have export on 2nd frame set in publishing settings, all checkboxes for export on 2nd frame unchecked in the assets, and this all worked before I changed the export on 2nd frame setting except it wasn't preloading 50% of the file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为发生的事情是这样的:
文档类始终加载在第一帧中,因为它代表您的 swf 根类,因此必须存在于第一帧中。现在,由于您将所有其他类导出到第 2 帧,我可以想象,LoadingSystem 仅从第 2 帧开始存在,但您尝试在文档类 Framework 的构造函数中实例化它。
您可以尝试的是,在 Framework 中创建一个“初始化”方法,并从第 2 帧的时间线中调用该方法。在该方法中,您将执行当前在 Framework 的构造函数中执行的操作。
i think what's happening is this:
A document class is ALWAYS loaded in the first frame, because it represents your swf root class and thus has to be there in the first frame. Now, since you export all the other classes to frame 2, i would imagine, that LoadingSystem is existing only beginning with frame two, but you try to instantiate it in the constructor of your document class Framework.
What you could try out is, create a method "initialize" in Framework and call that from the timeline in frame 2. And in that method you would do the stuff, you currently do in the constructor of Framework.
如果
myLogo
是舞台上的 sprite/movieclip,则在将LoadingSystem
添加到stage
之前它不会存在。现在你的第一反应应该是“但我用
parent.addChild(this)
将它添加到舞台了!”。您没有考虑到调用构造函数时文档类并不在舞台上。 Flash 基本上是这样执行的:这意味着文档类的
stage
属性将为 null,直到构造函数完成之后。这也意味着在构造函数期间添加的任何子级都无法访问舞台或位于舞台上的对象,直到将 docClass 添加到舞台之后。有一个简单的修复方法;监听
ADDED_TO_STAGE
事件。if
myLogo
is a sprite/movieclip on the stage, it wont exist untilLoadingSystem
is added to thestage
.Now your first reaction should be "but I added it to the stage with
parent.addChild(this)
!". What you didn't take into account is that the document class isn't on the stage when the constructor is called. Flash basically executes like this:Which means that the
stage
property of the document class will be null until after the constructor is finished. It also means that any children added during the constructor wont have access to the stage or objects located on the stage until after the docClass is added to the stage.There is a simple fix; listen for the
ADDED_TO_STAGE
event.