Actionscript 3 TypeError:AS3闪存cs6中的错误#1009
我在使用flash时出现以下错误: TypeError:错误#1009:无法访问空对象引用的属性或方法。 在选项()中 这是我的选项课:Actionscript 3 TypeError:AS3闪存cs6中的错误#1009,actionscript-3,typeerror,flash-cs6,Actionscript 3,Typeerror,Flash Cs6,我在使用flash时出现以下错误: TypeError:错误#1009:无法访问空对象引用的属性或方法。 在选项()中 这是我的选项课: package { import flash.display.MovieClip; import fl.managers.StyleManager; import flash.text.TextFormat; import fl.events.ComponentEvent; import fl.events.
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”。我怎样才能做到这一点?因为现在,我尝试将cPlayerData.pName默认设置为“Papoi”,但它不起作用。嗯..您的问题出在构造函数中,因此当您尝试访问其属性时,组件“msgDisplay”和/或组件“nameBox”可能尚未完全初始化。。。 一个好的做法是仅在对象完全初始化时访问对象,这可以使用事件“AddedToSatge”完成,在初始化所有子对象之前不会触发该事件。。 注意:即使这不是问题的根源,但始终这样做也是一件好事,因为它将使您避免与同一问题相关的其他问题和bug 更新: 问题出在
loadData()
函数中,因为您在该函数体中更改了SharedObject的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, "");
}
//────────────────────────────────────────────
}
}
嗯,我尝试将我的代码修改为您建议的代码,但仍然有错误,TypeError:Error#1009:无法访问null对象引用的属性或方法。at Options/init()at Options()TypeError:错误#1009:无法访问空对象引用的属性或方法。flash.events::EventDispatcher/dispatchEventFunction()在flash.events::EventDispatcher/dispatchEvent()在fl.controls::TextInput/handleKeyDown()在Options/setName()在flash.events::EventDispatcher/dispatchEventFunction()在fl.controls::TextInput/handleKeyDown()所有这些类型错误。此外,我刚刚更新了上面的代码,以。我不确定这是否与错误有关。。嗯..要获得有关错误的更多信息,如在大多数情况下非常有用的错误行号,您应该在文件>发布设置>Flash(.swf)>高级>允许调试中启用“允许调试”选项,如下图所示:,这将帮助我们更快地确定错误。哦,代码正在工作,但并非完全如此。部分工作,因为现在,当我在名称框中键入名称时,它会在消息显示中生成消息。但是,默认消息显示“向其他仆从介绍您自己!”仍然没有出现:/Hmmm。它有以下错误:TypeError:error#1009:无法访问空对象引用的属性或方法。在选项处/init()在选项处()