Juniki Takuya
Juniki Takuya

Reputation: 3

randomize/shuffle array item without repeat actionscript 3.0

i am creating a question and answer quiz using flash cs3 and actionscript 3.0. i have a large array of question, i wish to put it into xml document(can xml file be reside in the flash file itself?i thought ive seen someone did that.)

ok,my main problem is to shuffle the questions without repeat until all questions are asked. i have worked on this tutorial,and it does great shuffling without repeat. http://www.flashandmath.com/howtos/deal/

but,i wish to ask one question at once. i have looked into the option to shuffle frames, but i think about how can i count the score of the quiz at the end of it?

so i have edited my codes according to your answer,

i want to make a button named "check" to check if the answer filled is correct or wrong. and the score increase if the answer is right. the user will hit a button named "next" to go to the next question.

the final score will be shown at the next frame, after all questions were finished. will it be alright to bring the score counted to the next frame?

or, should i just make a movie clip visible with the score when all question ended?

check_btn.addEventListener(MouseEvent.CLICK, checkAnswer); next_btn.addEventListener(MouseEvent.CLICK, nextQuestion);

var index:int = 0;

var score:int = 0;

questions_txt.text = newQuizModel[index]["q"];

var userAnswer:String = "";

function checkAnswer(MouseEvent):void{

userAnswer = answers_txt.text;

if (userAnswer == newQuizModel[index]["a"])
{
    answers_txt.text = "";
    score++;
}
else
{
    answers_txt.text = "";
    score = score;
}

index++;
index%= quizModel.length;
nextQuestion(index);

}

function nextQuestion(idx:int):void{

for(var i:int=0; i<newquizModel.length; i++){

    if(i == quizModel.length - 1){
        nextFrame();
    }
    else{
        questions_txt.text = newQuizModel[idx]["q"];}

nextQuestion(index);

the code at the next frame,

score_txt.text = score.toString()+"/"+newQuizmodel.length;

i have noticed that user can fill the answer again if the answer is wrong.how do we give score only for user's first try?

many thanks :)

Upvotes: 0

Views: 868

Answers (1)

bitmapdata.com
bitmapdata.com

Reputation: 9600

Try this?

var quiz:Array = 
[
  "1+1 = ?",
  "5*5+5 = ?",
  "10/5*5-4 = ?",
  "12/6*6/12 = ?",
  "13+10/5-13/2 = ?"
];

function shuffleArray($arr:Array):Array
{
    var l:Number = $arr.length - 1;

    for (var it:uint = 0; it<l; it++)
    {
        var r:int = Math.round(Math.random() * l);
        var tmp:String = $arr[it];
        $arr[it] = $arr[r];
        $arr[r] = tmp;
    }
    return $arr;
}

var refreshQuiz:Array = shuffleArray(quiz);

trace(refreshQuiz);

So, why you try show quiz frame by frame? Using a actionscript code show. that is more simple and flexible. Make a quiz box as Textfield. and if user answer right, change the text.

Here is skeleton code.

var quizModel:Array = [{q:"1+1 = ?", a:"2"}, {q:"5+5 = ?", a:"10"}, {q:"2+2 = ?", a:"4"}, {q:"6+6 = ?", a:"12"},{q:"8-7 = ?",a:"1"}];

var user_ans:Array = new Array();
var newQuizModel:Array = shuffleArray(quizModel);
stage.addEventListener(KeyboardEvent.KEY_DOWN, go);

function shuffleArray(arr:Array):Array
{
    var l:Number = arr.length - 1;

    for (var it:uint = 0; it<l; it++)
    {
        var r:int = Math.round(Math.random() * l);
        var tmp:Object = arr[it];
        arr[it] = arr[r];
        arr[r] = tmp;
    }
    return arr;
}

var index:int = 0;
questions_txt.text = newQuizModel[index]["q"];
var userAnswer:String = "";
function go(k:KeyboardEvent):void
{
    if (k.keyCode != Keyboard.ENTER)
    {
        return;
    }

    userAnswer = answers_txt.text;

    if (userAnswer == newQuizModel[index]["a"])
    {
        answers_txt.text = "Your answer is correct!";
    }
    else
    {
        answers_txt.text = "Your answer is wrong";
        return;
    }

    index++;
    index%= quizModel.length;
    showQuiz(index);
}

function showQuiz(idx:int):void
{
    questions_txt.text = newQuizModel[idx]["q"];
}

showQuiz(index);

Upvotes: 1

Related Questions