TypeError:AS3 flash cs6中的错误#1009

时间:2013-10-23 16:27:27

标签: actionscript-3 typeerror flash-cs6

我在使用闪光灯时出现此错误:

TypeError:错误#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;
        }

    }

}

有谁知道为什么存在这个特殊错误?

而且,如果名称未在nameBox中输入,我想将cPlayerData.pName设为“Papoi”。我怎么做到这一点? Cuz现在,我尝试将cPlayerData.pName默认设置为“Papoi”,但它不起作用。嗯..

1 个答案:

答案 0 :(得分:1)

您的问题出现在构造函数中,因此当您尝试访问其中一个属性时,组件“msgDisplay”和/或组件“nameBox”尚未完全初始化... 一个好的做法是只有在完全初始化对象时才能访问它们,这可以使用事件“AddedToSatge”完成,在初始化所有子项之前不会触发该事件。 注意:即使它不是您问题的根源,也总是这样做是件好事,因为它可以避免您遇到与同一问题相关的其他问题和错误。

更新: 问题出在您的loadData()函数中,因为您更改了该函数体内的SharedObject的localPath(它与saveData()函数中使用的不同),然后是您加载的数据将始终为null,这是您在错误消息中看到的。你只需要从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, "");
        }
        //────────────────────────────────────────────
    }
}