类型错误:在AS3闪光灯CS6错误#1009错误、闪光灯、类型

2023-09-08 15:38:29 作者:青鹿

我有这个错误,当工作在我的闪存:

I have got this error while working on my flash:

类型错误:错误#1009:无法访问空对象引用的属性或方法。 在选项()

TypeError: Error #1009: Cannot access a property or method of a null object reference. at Options()

这是我的期权类:

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

    }

}

有谁知道为什么存在此特定错误?

Does anyone know why this particular error exists?

还有,我想使cPlayerData.pName是Papoi如果名字没有在namebox中输入。我怎样做到这一点?的Cuz现在,我试图通过默认情况下,cPlayerData.pName设置为Papoi,但它不工作。嗯..

And also, I want to make the cPlayerData.pName to be "Papoi" if a name is not entered in the nameBox. How am I to make that happen? Cuz right now, I tried by setting the cPlayerData.pName to "Papoi" by default, but it doesn't work. Hmm..

推荐答案

您的问题是在构造函数,所以也许组件msgDisplay和/或组件namebox中是/未初始化完全还没有,而你正在尝试访问它的属性之一... 一个好的做法是,访问你的对象只有当它们被完全初始化,可以使用事件AddedToSatge这不会的被初始化所有的孩子之前被解雇完成.. 注:即使它不是你的问题的根源,这是一个很好的事情总是因为这将节省您从其他相关同一个问题的问题和错误

Your problem is in the constructor function so maybe the component "msgDisplay" and/or the component "nameBox" are/is not initialized completely yet while you are trying to access one of its properties... A good practice is to access your objects only when they are fully initialized, that can be done using the event "AddedToSatge" which will not be fired before all children’s are initialized.. Note: even if it is not the source of your problem, it is a good thing to do always because it will save you from other problems and bugs related to the same issue.

更新: 问题是在你的 loadData()函数,因为你已经改变了的localPath 您的共享对象中的函数体内(在 SAVEDATA使用()函数),那么你的加载的数据永远是空,那是你的错误信息看到的却是不一样的。你只需要删除该行从loadData功能。看到我更新code以下。

UPDATE: The problem was in your loadData() function, because you have changed the localPath of your SharedObject inside that function body (it is not the same as used in saveData() function), then your loaded data will always be null, and that was what you see in the error message. you just need to delete that line from loadData function. see my updated code below.

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, "");
        }
        //────────────────────────────────────────────
    }
}
 
精彩推荐