繁体   English   中英

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

[英]preloading external .swf with class file as3

所以我试图使用此代码使外部预加载器加载我的主.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:错误#1009:无法访问空对象引用的属性或方法。 在mainLoading()

我想收到错误消息是因为我正在访问mainLoading()类文件中的阶段。 我尝试将其添加到类文件中的构造函数中,但没有成功:

public function mainLoading () {
    addEventListener(Event.ADDED_TO_STAGE, init);   
}
private function init(e:Event): void {
    initStartUpScene ();

}

我的initStartUpScene函数只是将介绍性场景抛出到loading.swf

有什么建议么?

谢谢你的帮助。

(问题)是你mainLoading将扩充SpriteMovieclip

编辑

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

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

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

content让你访问你的加载方法瑞士法郎主类(如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

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

如果这是您收到的确切消息,则可以,添加ADDED_TO_STAGE侦听器应已对其进行了修复。 如果您对它进行了任何更改,请记住重新编译“ loading.swf” (这一步我似乎总是忘记了)

单独运行“ loading.swf”是否正常(没有将其加载到“容器” SWF中)?

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

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);
}

如果您也不需要它,则可以删除它。

OOOOOOKKKKK,所以在承认失败大约一个星期后,再次尝试,重写了几乎一半的代码,试图使自己相信预加载器被高估了,我终于弄清楚了(请打鼓):

在调用构造函数方法之前,我有一个变量引用正在调用的阶段。 像这样:

私有var xScrollPosRight:Number = stage.stageWidth-xScrollPosLeft;

我只是将stage.stageWidth更改为750,它才起作用。

活到老,学到老。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM