2012-11-13 65 views
0

我在ActionScript 3中製作遊戲。我有一個Menu類和一個呈現菜單的方法。 我在我的Main類中創建了一個Menu實例,然後調用該方法。當我調試應用程序時,我得到一個空引用錯誤。這是菜單類的代碼:AS3:錯誤1009:空引用

package 
{ 
import flash.display.MovieClip; 

import menucomponents.*; 

public class Menu extends MovieClip 
{ 
    public function Menu() 
    { 
     super(); 
    } 

    public function initMenuComponents():void{ 
     var arrMenuButtons:Array = new Array(); 

     var btnPlay:MovieClip = new Play(); 
     var btnOptions:MovieClip = new Options(); 
     var btnLikeOnFacebbook:MovieClip = new LikeOnFacebook(); 
     var btnShareOnFacebook:MovieClip = new ShareOnFacebook() 

     arrMenuButtons.push(btnPlay); 
     arrMenuButtons.push(btnOptions); 
     arrMenuButtons.push(btnLikeOnFacebbook); 
     arrMenuButtons.push(btnShareOnFacebook); 

     var i:int = 0; 

     for each(var item in arrMenuButtons){ 
      item.x = (stage.stageWidth/2) - (item.width/2); 
      item.y = 100 + i*50; 
      item.buttonMode = true; 

      i++; 
     } 
} 
} 
} 

在此先感謝。

+0

難道你沒有[只是問這個問題](http://stackoverflow.com/questions/13363504/as3-error-1009)? –

+0

使用調試器來查看發生了什麼,如果您沒有方便的調試器,則可以嘗試至少保留日誌 –

+0

@SamIam我在嘗試向舞臺添加項目的行處出現錯誤。這是否意味着階段爲空或我的項目爲空?對不起,如果這是一個愚蠢的問題,我從來沒有在我的大學學習正確的調試。 –

回答

0

您的問題可能是您的for循環運行時階段尚未填充。請嘗試以下操作:

public class Main extends MovieClip { 
    public function Main() { 
     super(); 
     var menu:Menu = new Menu(); 

     //it's good practice - as sometimes stage actually isn't populated yet when your main constructor runs - to check, though in FlashPro i've never actually encountered this (flex/flashBuilder I have) 
     if(stage){ 
      addedToStage(null); 
     }else{ 
      //stage isn't ready, lets wait until it is 
      this.addEventListener(Event.ADDED_TO_STAGE,addedToStage); 
     } 
    } 

    private function addedToStage(e:Event):void { 
     menu.addEventListener(Event.ADDED_TO_STAGE,menuAdded); //this will ensure that stage is available in the menu instance when menuAdded is called. 
     stage.addChild(menu); 
    } 

    private function menuAdded(e:Event):void { 
     menu.initMenuComponents(); 
    } 
} 
+0

已經嘗試過,發現這在其他網站上,沒有工作。我終於解決了這個問題。 在一個網站發現了一個邪惡的修復程序,它的工作很神奇。將文件保存爲AS2,然後保存爲AS3。工作。 –

+0

您的問題不是編程問題,而是FlashPro問題。儘管這是一個真正的解決辦法,但我仍然懷疑。 – BadFeelingAboutThis