2013-10-23 84 views
-1

在我的閃光燈工作時,我有這個錯誤:類型錯誤:錯誤#1009在AS3閃存CS6

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

這是我的選項類:

package { 

    import flash.display.MovieClip; 
    import fl.managers.StyleManager; 
    import flash.text.TextFormat; 
    import fl.events.ComponentEvent; 
    import fl.events.*; 
    import flash.events.MouseEvent; 
    import fl.controls.*; 
    import flash.net.SharedObject; 
    import flash.events.Event; 

    public class Options extends MovieClip 
    { 
     //DECLARE CLASS VARIABLES// 
     //DECLARE COMPONENT VARIABLES 
     private var cComponentFmt:TextFormat; 
     //DECLARE SAVE DATA VARIABLES 
     private var cPlayerData:Object; 
     private var cSavedGameData:SharedObject; 
     //DECLARE SOUND VARIABLES 

     public function Options() 
     { 
      //created the SharedObject using the getLocal() function 
      cSavedGameData = SharedObject.getLocal("savedPlayerData"); 
      //set component formats 
      setComponents(); 
      //initialize player's data 
      setPlayerData(); 
      //set default message display upon entering the setup page 
      msgDisplay.text = "Introduce yourself to the fellow minions!"; 
      //add event listeners 
      nameBox.addEventListener(MouseEvent.CLICK, clearName); 
      nameBox.addEventListener(ComponentEvent.ENTER, setName); 
     } 

     private function setComponents():void 
     { 
      //set the TextFormat for the components 
      cComponentFmt = new TextFormat(); 
      cComponentFmt.color = 0x000000; //black colour 
      cComponentFmt.font = "Comic Sans MS"; //set default "Comic Sans MS" 
      cComponentFmt.size = 16; 
      //apply the new TextFormat to ALL the components 
      StyleManager.setStyle("textFormat", cComponentFmt); 
     } 

     private function setName(evt:ComponentEvent):void 
     { 
      trace("Processing text input box.."); 
      // player pressed the ENTER key 
      cPlayerData.pName = nameBox.text; 
      msgDisplay.text = "Welcome, " + cPlayerData.pName + "! \nEnjoy many hours of fun with us!"; 
      saveData(); 
     } 

     private function clearName(evt:MouseEvent):void 
     { 
      // player CLICKED in the nameBox 
      nameBox.text = ""; 
     } 

     private function setPlayerData():void 
     { 
      //all variables that relate to the player 
      cPlayerData = new Object(); 
      //options related variables 
      cPlayerData.pName = "Papoi"; 
      cPlayerData.sndTrack = "none"; 
      //game related variables 
      cPlayerData.pScore = 0; 
      //save the player's data 
      saveData(); 
     } 

     private function saveData():void 
     { 
      //savedPlayerData = cPlayerData is the name=value pair 
      cSavedGameData.data.savedPlayerData = cPlayerData; 
      //force Flash to update the data 
      cSavedGameData.flush(); 
      //reload the newly saved data 
      loadData(); 
     } 

     private function loadData():void 
     { 
      //gets the data stored in the SharedObject 
      //this particular line not found in the options.as 
      cSavedGameData = SharedObject.getLocal("savedPlayerData","/",false); 
      //now stores the save data in the player object 
      cPlayerData = cSavedGameData.data.savedPlayerData; 
     } 

    } 

} 

有誰知道爲什麼存在這種特殊的錯誤?

此外,我想使cPlayerData.pName爲「Papoi」,如果沒有在名稱框中輸入名稱。我怎麼做到這一點? Cuz現在,我試圖通過默認設置cPlayerData.pName爲「Papoi」,但它不起作用。嗯..

回答

1

你的問題是在構造函數,所以也許組件「msgDisplay」和/或組件「nameBox」/尚未完全初始化,而你試圖訪問它的一個屬性... 一個好的做法是隻有在完全初始化的時候才能訪問你的對象,這可以使用事件「AddedToSatge」來完成,這個事件在所有的孩子被初始化之前不會被觸發。 注意:即使它不是問題的根源,總是這樣做是件好事,因爲它可以幫助您避免與同一問題相關的其他問題和錯誤。

UPDATE: 問題是在你loadData()功能,因爲你已經改變了你的共享對象是函數體內的localPath(它不是在saveData()功能使用的相同),那麼你的加載的數據永遠是空,這就是你在錯誤信息中看到的。你只需要從loadData函數中刪除該行。請參閱下面我的更新代碼。

package 
{ 
    import flash.display.MovieClip; 
    import fl.managers.StyleManager; 
    import flash.text.TextFormat; 
    import fl.events.ComponentEvent; 
    import fl.events.*; 
    import flash.events.MouseEvent; 
    import fl.controls.*; 
    import flash.net.SharedObject; 
    import flash.events.Event; 

    public class Options extends MovieClip 
    { 
     //DECLARE CLASS VARIABLES// 
     //DECLARE COMPONENT VARIABLES 
     private var cComponentFmt:TextFormat; 
     //DECLARE SAVE DATA VARIABLES 
     private var cPlayerData:Object; 
     private var cSavedGameData:SharedObject; 
     //DECLARE SOUND VARIABLES 

     public function Options() 
     { 
      if (stage) 
      { 
       init(); 
      } 
      else 
      { 
       addEventListener(Event.ADDED_TO_STAGE, init); 
      } 
     } 

     public function init(e:Event = null):void 
     { 
      // it is important to remove it coz you don't need it anymore: 
      removeEventListener(Event.ADDED_TO_STAGE, init); 

      //created the SharedObject using the getLocal() function 
      cSavedGameData = SharedObject.getLocal("savedPlayerData"); 
      //set component formats 
      setComponents(); 
      //initialize player's data 
      setPlayerData(); 
      //set default message display upon entering the setup page 
      msgDisplay.text = "Introduce yourself to the fellow minions!"; 
      //add event listeners 
      nameBox.addEventListener(MouseEvent.CLICK, clearName); 
      nameBox.addEventListener(ComponentEvent.ENTER, setName); 
     } 

     private function setComponents():void 
     { 
      //set the TextFormat for the components 
      cComponentFmt = new TextFormat(); 
      cComponentFmt.color = 0x000000;//black colour 
      cComponentFmt.font = "Comic Sans MS";//set default "Comic Sans MS" 
      cComponentFmt.size = 16; 
      //apply the new TextFormat to ALL the components 
      StyleManager.setStyle("textFormat", cComponentFmt); 
     } 

     private function setName(evt:ComponentEvent):void 
     { 
      trace("Processing text input box.."); 
      // player pressed the ENTER key 

      // remove the whitespace from the beginning and end of the name: 
      var playerNameWithoutSpaces:String = trimWhitespace(nameBox.text); 
      // check if the user did not enter his name then default name is "Papoi": 
      if (playerNameWithoutSpaces == "") 
      { 
       cPlayerData.pName = "Papoi"; 
      } 
      else 
      { 
       cPlayerData.pName = nameBox.text; 
      } 

      //// This will replace the default message : 
      //// msgDisplay.text = "Welcome, " + cPlayerData.pName + "! \nEnjoy many hours of fun with us!"; 
      // This will add the welcome message to the default message : 
      msgDisplay.text += "\nWelcome, " + cPlayerData.pName + "! \nEnjoy many hours of fun with us!"; 
      saveData(); 
     } 

     private function clearName(evt:MouseEvent):void 
     { 
      // player CLICKED in the nameBox 
      nameBox.text = ""; 
     } 

     private function setPlayerData():void 
     { 
      //all variables that relate to the player 
      cPlayerData = new Object(); 
      //options related variables 
      cPlayerData.pName = "Papoi"; 
      cPlayerData.sndTrack = "none"; 
      //game related variables 
      cPlayerData.pScore = 0; 
      //save the player's data 
      saveData(); 
     } 

     private function saveData():void 
     { 
      //savedPlayerData = cPlayerData is the name=value pair 
      cSavedGameData.data.savedPlayerData = cPlayerData; 
      //force Flash to update the data 
      cSavedGameData.flush(); 
      //reload the newly saved data; 
      loadData(); 
     } 

     private function loadData():void 
     { 
      //gets the data stored in the SharedObject 
      //this particular line not found in the options.as 

      //// delete the next line, no need to set it every time : 
      //// cSavedGameData = SharedObject.getLocal("savedPlayerData","/",false); 

      //now stores the save data in the player object 
      cPlayerData = cSavedGameData.data.savedPlayerData; 
     } 
     //──────────────────────────────────────────── 
     private function trimWhitespace($string:String):String 
     { 
      if ($string == null) 
      { 
       return ""; 
      } 
      return $string.replace(/^\s+|\s+$/g, ""); 
     } 
     //──────────────────────────────────────────── 
    } 
} 
+0

嗯,我想我的修改代碼以您所提出的一個,但它仍然有錯誤,類型錯誤:錯誤#1009:無法訪問空對象的屬性或方法reference.at選項/的init( )在Options()TypeError:錯誤#1009:無法訪問空對象引用的屬性或方法。 \t在選項/ setname可以() \t在flash.events::EventDispatcher/dispatchEventFunction() \t在flash.events::EventDispatcher/dispatchEvent() \t在fl.controls ::的TextInput/handleKeyDown()所有這些TypeErrors 。 –

+0

此外,我剛剛更新了我的代碼,以獲取加載和保存數據。我不確定這是否與錯誤有關..嗯.. –

+0

要獲得有關錯誤的更多信息,如在大多數情況下非常有用的錯誤行號,應在File中啓用「Permit Debugging」(允許調試)選項>發佈設置> Flash(.swf)>高級>允許調試,如在此圖像中:http://i.stack.imgur.com/EHgtK.jpg,這將幫助我們更快地確定您的錯誤。 – Amer