Joshua Wood
Joshua Wood

Reputation: 1

Flash Resetting array values

I was working on a test project that I will later incorporate into a much larger work that is a simple quiz game. I made an array with my questions:

var questions1:Array=["nitrogen dioxide","sulfur hexafluoride",..."]

and in a second layer I made a button that cycles through the questions randomly.

import flash.events.MouseEvent;

var qno=0;var rnd1;
function change_question(){
rnd1=Math.ceil(Math.random()*questions1.length)-1;
q.text=questions1[rnd1];
if(questions1[rnd1]=="X"){change_question();}
questions1[rnd1]="X";
}
change_question();

next_b.addEventListener(MouseEvent.CLICK, ButtonAction1);
function ButtonAction1(eventObject:MouseEvent){
qno++;change_question();

}

This part works great, because I was following a tutorial. Text appears in a dynamic text box I created as it should. This tutorial taught to change the value of the array to X with every choice and to ignore choose a different question every time it encountered an X

After it cycles through all the questions I basically get an infinite loop in my output section of flash because it can't find any more non-X values. I was hoping someone had information on how to press a button an change the array back to its default settings so that a teacher (because that is who it is for) has a way of resetting the file when they have reached the end of the quiz.

Thanks everyone!

Upvotes: 0

Views: 79

Answers (1)

Randhir Kumar
Randhir Kumar

Reputation: 197

As per my understanding you wanted to randomize your questions and after showing all the entire questions you wanted to reset your questions.

As per your code you get a random question and updating the same array by pushing value 'X'. Rather than doing this what you need to do is to preserve the array only randomize its position. So that you can use the same value once you cover your all questions

I have added code here.

import flash.events.MouseEvent;

var qno=0;var rnd1;
var questions1:Array=["nitrogen dioxide","sulfur hexafluoride","carbon dioxide","carbon monooxide"];
var nAttmeptedCount = 0;
var shuffledLetters:Array;
function change_question()
{
    if(qno == questions1.length)
    {
        qno = 0;
        resetQuestion()
    }
    else
    {
        q.text = questions1[shuffledLetters[qno]];
        qno++;      
    }
}

function resetQuestion()
{

    shuffledLetters = new Array(questions1.length);
    for(var i=0;i<shuffledLetters.length;i++)
    {
        shuffledLetters[i] = i;
    }

    shuffledLetters.sort(randomize);
}

function randomize ( a : *, b : * ) : int {
    return ( Math.random() > .5 ) ? 1 : -1;
}


resetQuestion()
change_question();

next_b.addEventListener(MouseEvent.CLICK, ButtonAction1);
function ButtonAction1(eventObject:MouseEvent){
change_question();

}

In above solution after showing all questions I have reset the questions automatically. you can modify code as per your requirement. If you want to do the reset questions you can put you code qno = 0;resetQuestion() on button click.

hope above solution work for you.

Upvotes: 1

Related Questions