簡體   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