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?";
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!
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 = "wrong";
} else {
display = "correct";
}
}
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.