我在为我的flash项目构建的命令系统中遇到了一些错误。其中一个命令是介绍,它基本上重新启动项目,或者意味着(介绍从第一个场景的第0帧开始)。但是当它运行时,引用错误被抛出我已经确定为第21行(通过注释代码直到它不再出错),这是引入函数的thisRef.gotoAndPlay行。
这是我的代码:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.events.KeyboardEvent;
import flash.media.Sound;
public class Main extends MovieClip {
/* The real frame number */
public var realFrameNo:int = 0;
/* Whether startup has been ran or not */
public var startupRan:Boolean = false;
/* Whether or not the beggining text is there */
public var placeholderPresent:Boolean = true;
/* Commands recognized by the computer [command:string,handler:void] */
public var commandList = new Array(["intro",function(thisRef:Main) {
gotoAndPlay(0,"Intro");
}],
["exit",function(thisRef:Main) {
fscommand("quit");
}]);
/* Helper functions */
public function isAlphabetic(value:String):Boolean
{
var result:String = value.match(/[a-zA-Z]*/)[0];
return value.length == result.length;
}
public function getSceneNo():int
{
return (realFrameNo > 0 ? Math.floor( (realFrameNo / stage.frameRate) ) : 0 );
}
public function getSceneName(sceneNo:int):String
{
switch(sceneNo)
{
default:
return "Intro";
case 0:
return "Intro";
case 1:
return "MainMenu";
}
}
/* Main functions */
public function Main()
{
this.addEventListener('enterFrame',function() {
if(!this.startupRan)
realFrameNo += 1;
/* Main menu init */
if(getSceneNo() == 1 && !this.startupRan && currentFrame == 2)
{
stop();
this.startupRan = true;
/* Add keyboard events */
stage.addEventListener(KeyboardEvent.KEY_DOWN,function(event:KeyboardEvent){
var letter:String = String.fromCharCode(event.charCode).toUpperCase();
if(event.keyCode == 8)
updateMonitor("");
else if(event.keyCode == 13)
evalInput();
else if(isAlphabetic(letter))
updateMonitor(letter);
});
/* Add button events */
btnQ.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("Q"); });
btnW.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("W"); });
btnE.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("E"); });
btnR.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("R"); });
btnT.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("T"); });
btnY.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("Y"); });
btnU.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("U"); });
btnI.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("I"); });
btnO.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("O"); });
btnP.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("P"); });
btnA.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("A"); });
btnS.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("S"); });
btnD.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("D"); });
btnF.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("F"); });
btnG.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("G"); });
btnH.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("H"); });
btnJ.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("J"); });
btnK.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("K"); });
btnL.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("L"); });
btnZ.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("Z"); });
btnX.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("X"); });
btnC.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("C"); });
btnV.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("V"); });
btnB.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("B"); });
btnN.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("N"); });
btnM.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("M"); });
btnDel.addEventListener(MouseEvent.CLICK,function(){ updateMonitor(""); });
btnEnter.addEventListener(MouseEvent.CLICK,function(){ evalInput(); });
btnReturn.addEventListener(MouseEvent.CLICK,function(){ evalInput(); });
}
});
}
public function updateMonitor(letter:String)
{
if(this.placeholderPresent) //Remove placeholder
lblInput.text = letter;
else if(letter == '' || letter == null) //Backspace
{
lblInput.text = lblInput.text.substr(0,lblInput.text.length-1);
}
else //Append text
lblInput.appendText(letter);
if(lblInput.text == "")
{
lblInput.text = "Type something to begin..";
this.placeholderPresent = true;
}
else
this.placeholderPresent = false;
}
public function evalInput():void
{
if(this.placeholderPresent) //Don't eval the starting text
return;
lblOutput.text = "Scanning for commands...";
for(var i:int = 0; i < this.commandList.length; i++)
{
if(lblInput.text.toLowerCase() == this.commandList[i][0])
{
this.commandList[i][1](this);
lblOutput.appendText("\nCommand ran.");
return;
}
}
lblOutput.appendText("\nNo command recognized.");
}
}
}
完整堆栈跟踪:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main/evalInput()[H:\Year 2\Units\Unit 43 - Multimedia Design\Flash Application\Main.as:144]
at Function/<anonymous>()[H:\Year 2\Units\Unit 43 - Multimedia Design\Flash Application\Main.as:71]