使用类文件 as3 预加载外部 .swf

发布于 2024-10-23 16:40:25 字数 1013 浏览 1 评论 0原文

所以我试图创建一个外部预加载器来加载我的主 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柳絮泡泡 2024-10-30 16:40:25

(问题)您的 mainLoading 是否扩展了 SpriteMovieclip

编辑

阅读您的评论后,我建议尝试以下操作:

在完整进度处理程序中的 swf 内容内添加一个函数调用:

function done (e:Event):void{
    removeChild(loadingPage);
    addChild(l);
    Object(l.content).initMethod();
}

content 让您访问加载的 .swf 主类中的方法(例如 mainLoading)

并将 mainLoading 中的事件处理替换为:

public function mainLoading () {
    //no event handling in constructor
}

public function initMethod():void {
    //here you go
    init();
}

public function init():void { ... //No more event here

顺便说一句,这不是解决问题的最干净的方法。

(question) is your mainLoading extends either Sprite or Movieclip ?

EDIT

After reading your comment, I would suggest trying this :

Add a function call inside the swf content in your complete progress handler :

function done (e:Event):void{
    removeChild(loadingPage);
    addChild(l);
    Object(l.content).initMethod();
}

content let you access the methods in your loaded .swf main class (e.g. mainLoading)

And replace the event handling in your mainLoading by :

public function mainLoading () {
    //no event handling in constructor
}

public function initMethod():void {
    //here you go
    init();
}

public function init():void { ... //No more event here

Btw it's not the cleanest way to solve your problem.

乖乖哒 2024-10-30 16:40:25

如果这就是您收到的确切消息,那么是的,添加 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)?

述情 2024-10-30 16:40:25

这可能与您提出的问题无关,但通过像这样构造代码,您可能会获得更好的结果并避免一些错误:

var loadingPage:loading = new loading;
addChild(loadingPage);
loadingPage.x = stage.stageWidth/2;
loadingPage.y = stage.stageHeight/2;

var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
l.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
l.load(new URLRequest("loading.swf"));

//I renamed this function since I believe "loop" is a reserved keyword.
function onProgress (e:ProgressEvent):void{
    //No code needed
}

function onComplete (e:Event):void{
    removeChild(loadingPage);
    addChild(l);
}

如果您也不需要“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:

var loadingPage:loading = new loading;
addChild(loadingPage);
loadingPage.x = stage.stageWidth/2;
loadingPage.y = stage.stageHeight/2;

var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
l.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
l.load(new URLRequest("loading.swf"));

//I renamed this function since I believe "loop" is a reserved keyword.
function onProgress (e:ProgressEvent):void{
    //No code needed
}

function onComplete (e:Event):void{
    removeChild(loadingPage);
    addChild(l);
}

You can remove the "onProgress" function if you won't be needing it as well.

把人绕傻吧 2024-10-30 16:40:25

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文