Creating a Multiple Choice Quiz Using XML, Part 2
July 24, 2001
by Will Turnage
Last week, you learned how to take an XML file and import it into Director. You converted the XML into a list and learned how to make the questions and/or the answers appear in a random order. This week, you're going to learn how to program the quiz and keep track of the user's score. The first step is to create one behavior that will go on each of the four multiple choice answers in the quiz.
global gQuestions
on mouseUp me
tempText = sprite (me.spriteNum).member.text
delete char 1 to 3 of tempText
gQuestions.answerQuestion (tempText)
end
When you click on one of the four answers, it triggers the mouseUp handler in this behavior. The first line of code takes the text inside the member you clicked on and put it in the variable called tempText. Next, it deletes the first three characters from the string. The reason it deletes the characters is to get rid of the multiple choice letter at the beginning of each answer. So while you see an answer displayed as "a) Will Turnage", by deleting the first three characters, you've turned it into "Will Turnage" for easier verification. Finally, the behavior sends the answer you clicked on to the answerQuestion handler. The answerQuestion handler looks like this:
on answerQuestion me, theAnswer
if theAnswer = pQuestionList[pCurrentQuestion].correct then
pCorrect = pCorrect + 1
else
pIncorrect = pIncorrect + 1
end if
member ("numberCorrect").text = "Correct:" && pCorrect
member ("numberIncorrect").text = "Incorrect:" && pIncorrect
if pCurrentQuestion = pQuestionList.count then
go "gameover"
else
me.viewNextQuestion ()
end if
end
This handler first checks to see if the answer passed to it is equal to the correct answer for the current question that you're viewing. If it is the correct answer, then it increases your correct score by one. Otherwise it increases your incorrect score by one. Next, it takes the new scores for the number of correct and incorrect answers and puts them into two text members on screen. This way, you can easily see how many questions you've gotten right or wrong. Finally, the handler checks to see if you're at the last question in the list. If you are, then it goes to a different frame in the movie called 'gameover', otherwise, it loads the next question.
And that's really all there is to making a quiz. Not too complicated, right? Well, the real challenge here is taking this code framework and extending it to fit the requirements of your own project. Sometimes those changes can be easy, and sometimes they can be harder.
Say for example, you wanted to add a feedback feature to the quiz. That way when a person clicks on an answer, it tells you whether you got it right or wrong, and then waits two seconds before moving on to the next question. Not too hard, right?
The first step to creating feedback is to add a sprite to the score that will display whether you got the question right or wrong. Once you've done that, then you're ready to change your code. In this instance, you want to take the answerQuestion handler and break it into two smaller handlers like this.
on answerQuestion me, theAnswer
if theAnswer = pQuestionList[pCurrentQuestion].correct then
pCorrect = pCorrect + 1
sprite (10).member = member ("correctFeedback")
else
pIncorrect = pIncorrect + 1
sprite (10).member = member ("incorrectFeedback")
end if
sprite (10).loc = point (350, 80)
member ("numberCorrect").text = "Correct:" && pCorrect
member ("numberIncorrect").text = "Incorrect:" && pIncorrect
timeout ("clearFeedback").new (2000, #clearFeedback, me)
end
on clearFeedback me
sprite (10).loc = point (5000, 5000)
if pCurrentQuestion = pQuestionList.count then
go "gameover"
else
me.viewNextQuestion ()
end if
timeout ("clearFeedback").forget ()
end
In this new answerQuestion handler, the code is mostly the same, except now if you get the question right, then you set the member of your feedback sprite (in this case sprite 10) to the appropriate member. The next line of code moves the feedback sprite into a position on screen where the user has a chance to see it. You update your scores like before, but now instead of moving on to the next question, you create a new timeout object. This timeout will wait two seconds before executing the clearFeedback handler.
After two seconds have passed, the timeout object you created will call the clearFeedback handler. This handler will take your feedback sprite and move it off-screen. After the screen has been cleared, it checks to see if you're on the last question. If so, then the game's over, otherwise it loads up the next question. Finally -- and this is very important -- you delete the timeout object you created in the answerQuestion handler. When everything is finished, the final result looks like this:
A sample Director 8 movie and XML file (as well as an HTML and DCR file) are available for download in Macintosh or Windows format. Because this movie accesses external files, running from your hard drive within a browser requires that the movie path contain dswmedia.
Copyright 1997-2024, Director Online. Article content copyright by respective authors.