2011-03-19 55 views
0

所以我試圖讓外部預加載器加載我的主要瑞士法郎(loading.swf),有使用該代碼的類文件名爲mainLoading.as文件:與預加載的類文件AS3外部瑞士法郎

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

所以我得到一個錯誤信息說:

類型錯誤:錯誤#1009:無法訪問空對象引用的屬性或方法。 在mainLoading()

我想我得到的錯誤消息,因爲我在訪問我的mainLoading()類文件的階段。我想在我的類文件添加以下內容到構造,但沒有奏效:

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

} 

我initStartUpScene功能只是拋出簡介場景到loading.swf

有什麼建議?

感謝您的幫助。

+0

你確定'mainLoading()'方法中唯一的代碼只是對'addEventListener()'方法的調用,而沒有其他的方法嗎?如果不是,顯示其餘的缺少代碼可能會使您更容易識別您的問題。 – Taurayi 2011-03-19 09:11:13

回答

0

(問題)是您的mainLoading是否延伸SpriteMovieclip

編輯

閱讀您的評論後,我會建議嘗試這樣的:

添加一個函數調用的SWF內容裏面你完全進行處理:

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

content讓你訪問方法在你的加載.swf主類(例如mainLoading)

並替換mainLoad中的事件處理荷蘭國際集團通過:

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

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

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

順便說一句這不是解決你的問題最徹底的方法。

+0

@Taurayi - 是的,這是我在'mainLoading()'方法中的所有代碼。在'initStartUpScene()'函數中,我添加一個影片剪輯到'this.addChild();'我有一個'stage.addEventListener()'鍵盤事件。我發現這個:http://www.ghostwire.com/blog/archives/as3-avoiding-null-object-reference-error-when-loading-module-swf/這應該是解決這個問題,但它仍然沒有'爲我工作。 @Nebu - mainLoading擴展Movieclip。這會改變什麼嗎?非常感謝你的幫助。 – 2011-03-19 18:52:33

0

如果這是確切的消息,那麼是的,添加ADDED_TO_STAGE偵聽器應該修復它。 記住重新編譯「loading.swf」如果你做出任何更改(步我似乎總是忘了)

是否「loading.swf」在它自己的運行時,它沒有任何錯誤,運行得很好(沒有將它加載到「容器」SWF中)?

0

這可能是無關的,你問的問題,但你可能會得到更好的效果,並通過構建這樣的代碼避免一些錯誤:

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」功能,如果你贏了」也不需要它。

0

OOOOOOKKKKK,所以承認失敗,再嘗試它,重寫我的代碼幾乎一半,試圖說服自己的一個星期左右即預加載器被高估了,我終於想通了(請打起鼓)後:

我有一個變量引用在我的構造函數方法被調用之前被調用的階段。像這樣:

private var xScrollPosRight:Number = stage.stageWidth - xScrollPosLeft;

我只是將stage.stageWidth更改爲750,它工作。

生活和學習。

相關問題