0

我正在做一个问答游戏,当玩家按字母顺序输入时,系统会将输入与正确答案进行比较。我使用一个数组来存储所有问题,系统应该从数组的第一个索引加载元素,即“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;

        }


    }


}

}

4

2 回答 2

0

In displayQuestion you are assigning a value to answerArr. However, you are doing this in a for loop. This means that you iterate through all the elements and you change the value of that variable. That's why you get the latest answers directly.

于 2013-09-03T10:50:35.597 回答
0

正如 Krasimir 试图解释的那样,在“for”循环的最后一次迭代中,answerArr 被设置为仅包含 questionArr 数组中最后一项的拆分字母。您的其余代码永远不会看到 answerArr 包含除此之外的任何内容。我建议使用数组数组,以便 answerArr 数组的每个索引都包含一个由 questionArr 中的拆分字母组成的数组。因此,在 for 循环中:

answerArr.push(myStr.split("_"));

早些时候,您需要按如下方式声明 answerArr ...

 var answerArr:Array = new Array();

...因此它被定义为要“推送”到的 Array 对象。

于 2013-09-03T11:45:43.300 回答