Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

arrays in flash 2

Status
Not open for further replies.

greg50

IS-IT--Management
Jul 27, 2000
7
AU
Has anyone got a script for using arrays to make a quiz. I want 10 questions randomly displayed in a dynamic text box and the user's answers typed into another text box then validated.
 
You need something like this (this is a close enough approximation... and I haven't actually tried it - but it's near enough! =) )

first off you need to set-up your questions and answers... I don't think that Flash deals with multi-dimensioned arrays (don't forget that Flash arrays are zero based, so 0 is position 1 and 9 is position 10!).

--------------------
This bit sets up the arrays... it's probably worth loading these from a text file (you can construct a variable name using something like 'question_array[1] = eval(question + 1)' this adds the '1' to 'question' and then puts the contents of question1 into the array, at position 1).

Anyway, the basic array set-up (ignoring the bit I just mentioned above) is...

// set-up questions...
question_array[0] = "What is my name?";
.
.
question_array[9] = "Why are we here?";

// set-up answers...
answer_array[0] = "Petitpal";
.
.
answer_array[9] = "Who knows!";


-------------------------------------

Now when you want to load the question onto screen do this...

// get a random number...
my_random = random(9);

// set text box mc instance to question...
queston_textbox = question_array[my_random];

-------------------------------------

Fianlly, to validate the answer simply read the same position from the answer array...

// validate answer...
if (answer_array[my_random] != answer_textbox) {

// they got it wrong...
<insert code here>

} else {

// they got it right...
<insert code here>
}

--------------------------

Obviously this is very simple and doesn't take account of different case, etc, but it should give you a good starting point. =)

Good luck, and if you need any more help give me a shout!

=)

PetitPal.
 
Good one Petitpal and thanks for such a detailed response. Your script for validating random questions in arrays works just fine. I put the following script on a button:
on (release) {
if (answer[my_random]!=stud_answer) {
display = &quot;wrong&quot;;
} else {
display = &quot;correct&quot;;
}
}
The only remaining problem is duplicating questions each time I randomize my question array. Any suggestions?
greg50
 
OK, you have have a couple of options (so pick the one you fancy!)

You can either...
1) When you have asked them the question balnk out the entry in the question array; then put a check on your random number thingy to ensure that the question isn't blank - if it is then find another random number... (As the questions are initialized only when the movie loads, this should be OK).

2) The second option is to have (yes you guessed it) a third array which stores the question numbers already asked; this also has the advantage that you can later display the order they were asked the questions in, if you so desire.

Glad it worked anywa=)

PetitPal.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top