我正在做一个问答游戏,当玩家按字母顺序输入时,系统会将输入与正确答案进行比较。我使用一个数组来存储所有问题,系统应该从数组的第一个索引加载元素,即“F_L_A_S_H”,但它直接显示最后一个元素,谁能告诉我如何解决这个问题?
公共类主扩展 MovieClip {
var myRes:Array;
var answerArr:Array;
var questionNo:int = 0;
public function main() {
this.displayQuestion();
}
public function displayQuestion():void {
myRes = new Array();
answerArr = new Array();
var questionArr:Array = new Array("F_L_A_S_H", "H_E_L_L_O", "5_+_5_=_10");
for (var i:int =0; i<questionArr.length; i++) {
var myStr:String = questionArr[i];
answerArr = myStr.split("_");
var subStr:Array = myStr.split("_");
subStr.sort(this.randomSort);
word1.changelabel(subStr[0]);
word2.changelabel(subStr[1]);
word3.changelabel(subStr[2]);
word4.changelabel(subStr[3]);
word5.changelabel(subStr[4]);
stage.addEventListener(KeyboardEvent.KEY_DOWN,ClickDown);
}
}
public function ClickDown(e:KeyboardEvent):void{
alert1.changelabel('');
if(e.keyCode == 65){
myRes.push(word1.getLabel());
trace(myRes);
result1.changeLabel(myRes);
this.CheckAnswer(answerArr);
} else if (e.keyCode == 83) {
myRes.push(word2.getLabel());
trace(myRes);
result1.changeLabel(myRes);
this.CheckAnswer(answerArr);
} else if (e.keyCode == 68) {
myRes.push(word3.getLabel());
trace(myRes);
result1.changeLabel(myRes);
this.CheckAnswer(answerArr);
} else if (e.keyCode == 70) {
myRes.push(word4.getLabel());
trace(myRes);
result1.changeLabel(myRes);
this.CheckAnswer(answerArr);
} else if (e.keyCode == 71) {
myRes.push(word5.getLabel());
trace(myRes);
result1.changeLabel(myRes);
this.CheckAnswer(answerArr);
}
}
public function randomSort(objA:Object, objB:Object):int {
return Math.round(Math.random() * 3) - 1
}
public function CheckAnswer(newLabel:Array):void {
if( myRes.length > 4) {
for (var i:int = 0; i<myRes.length; i++) {
if (myRes[i] !== newLabel[i]) {
alert1.changelabel('You are Wrong!');
break;
} else {
alert1.changelabel('You are Right!');
}
}
myRes.length = 0;
}
}
}
}