0
votes

I am doing a quiz game, which when player type in the sequence of the letters, system will compare the input with the correct answer. I used an array to store all the questions and the system is supposed to load the element from the first index of the array, which is 'F_L_A_S_H', but it directly displays the last element, can anyone tell me how to fix this?

public class main extends 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;

        }


    }


}

}

2

2 Answers

0
votes

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.

0
votes

As Krasimir tried to explain, on the last iteration of the 'for' loop answerArr is being set to contain ONLY the split letters of the LAST item in the questionArr Array. The rest of your code never gets to see answerArr containing anything other than that. I'd suggest using an Array of arrays, so that each index of the answerArr Array contains an Array of the split letters in questionArr. So, in the for loop:

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

Earlier, you will need to declare answerArr as follows...

 var answerArr:Array = new Array();

...so it is defined as an Array object to 'push' to.