使用类文件 as3 预加载外部 .swf
所以我试图创建一个外部预加载器来加载我的主 .swf (loading.swf) 文件,该文件有一个名为 mainLoading.as 的类文件,使用以下代码:
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop);
l.contentLoaderInfo.addEventListener(Event.COMPLETE, done);
l.load(new URLRequest("loading.swf"));
var loadingPage:loading = new loading;
function loop (e:ProgressEvent):void{
addChild(loadingPage);
loadingPage.x = stage.stageWidth/2;
loadingPage.y = stage.stageHeight/2;
}
function done (e:Event):void{
removeChild(loadingPage);
addChild(l);
}
所以我收到一条错误消息:
TypeError: Error #1009 :无法访问空对象引用的属性或方法。 在 mainLoading() 处,
我认为我收到了错误消息,因为我正在访问 mainLoading() 类文件中的阶段。我尝试将其添加到我的类文件中的构造函数中,但它不起作用:
public function mainLoading () {
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event): void {
initStartUpScene ();
}
我的 initStartUpScene 函数只是将介绍场景抛出到 loading.swf
有什么建议吗?
感谢您的帮助。
so i'm trying to make an external preloader to load my main .swf (loading.swf) file that has a class file named mainLoading.as using this code:
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop);
l.contentLoaderInfo.addEventListener(Event.COMPLETE, done);
l.load(new URLRequest("loading.swf"));
var loadingPage:loading = new loading;
function loop (e:ProgressEvent):void{
addChild(loadingPage);
loadingPage.x = stage.stageWidth/2;
loadingPage.y = stage.stageHeight/2;
}
function done (e:Event):void{
removeChild(loadingPage);
addChild(l);
}
so I'm getting an error message saying:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at mainLoading()
I think i'm getting the error message because i am accessing the stage in my mainLoading() class file. I tried adding this to the constructor in my class file but it didn't work:
public function mainLoading () {
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event): void {
initStartUpScene ();
}
my initStartUpScene function just throws the intro scene to the loading.swf
any suggestions?
thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
(问题)您的
mainLoading
是否扩展了Sprite
或Movieclip
?编辑
阅读您的评论后,我建议尝试以下操作:
在完整进度处理程序中的 swf 内容内添加一个函数调用:
content
让您访问加载的 .swf 主类中的方法(例如 mainLoading)并将 mainLoading 中的事件处理替换为:
顺便说一句,这不是解决问题的最干净的方法。
(question) is your
mainLoading
extends eitherSprite
orMovieclip
?EDIT
After reading your comment, I would suggest trying this :
Add a function call inside the swf content in your complete progress handler :
content
let you access the methods in your loaded .swf main class (e.g. mainLoading)And replace the event handling in your mainLoading by :
Btw it's not the cleanest way to solve your problem.
如果这就是您收到的确切消息,那么是的,添加 ADDED_TO_STAGE 侦听器应该可以修复它。 如果对“loading.swf”进行任何更改,请记住重新编译(我似乎总是忘记这一步)
“loading.swf”单独运行时是否运行良好,没有任何错误(不将其加载到“容器”SWF 中)?
If that's the exact message you are getting, then yes, adding the ADDED_TO_STAGE listener should have fixed it. Remember to recompile the "loading.swf" if you make any changes to it (a step I always seem to forget)
Does "loading.swf" run just fine without any errors when running it on it's own (not loading it into the "container" SWF)?
这可能与您提出的问题无关,但通过像这样构造代码,您可能会获得更好的结果并避免一些错误:
如果您也不需要“onProgress”函数,则可以删除它。
This may be unrelated to the question you asked, but you might get better results and avoid some errors by structuring your code like this:
You can remove the "onProgress" function if you won't be needing it as well.
OOOOOOKKKKK,所以在大约一周的承认失败之后,再次尝试,重写了几乎一半的代码,试图说服自己预加载器被高估了,我终于弄清楚了(请击鼓):
我有一个变量引用了在调用我的构造函数方法之前调用阶段。像这样:
private var xScrollPosRight:Number = stage.stageWidth - xScrollPosLeft;
我刚刚将 stage.stageWidth 更改为 750 并且它起作用了。
生活和学习。
OOOOOOKKKKK, so after about a week of admitting defeat, trying it again, rewriting almost half of my code, trying to convince myself that preloaders are overrated, i finally figured it out (drum roll please):
I had a variable that was referencing the stage being called before my constructor method was called. like this:
private var xScrollPosRight:Number = stage.stageWidth - xScrollPosLeft;
I just changed stage.stageWidth to 750 and it worked.
live and learn.