Wow, we have gone a long way and are almost
done. All we need now is to do these:
- Check whether all the quiz has been
answered.
- Display the summary screen, showing
percentage of the correct answer.
CHECKING WHETHER ALL THE QUIZ HAS BEEN
ANSWERED
To do this part, we need to know how
many quiz items are in the XML file. When we parsed the XML data (see LOADING THE QUIZ DATA section
on the 1st page), we stored the questions
(quiz) in an Array named quizItems. We can just check the size of
this array to find out how many questions are on the XML file.
Where should we do the check? We can either do the check on the
"Correct" or "Wrong" frames, but
then we'll need to do the check on both places. If we look at the code
on both of those frames (see previous sections), they both call gotoAndStop("ShowQuiz"),
so it's better to do it there.
Here's what we need to insert into the beginning of the existing code
in the "Actions" layer of "ShowQuiz"
frame.
if (currentQuestionNumber>quizItems.length)
gotoAndStop("SummaryScreen");
|
So the action will look like this:
That added code checks the currentQuestionNumber against the number
of questions on the XML file. It also tells Flash to jump to a
frame labeled "SummaryScreen" when all the questions
has been shown.
CREATING THE SUMMARY SCREEN
As before, create a key frame at frame 50 or so (make sure
this frame is not overlapping the last frame created in any of the
previous steps), then in the Labels
layer, label the frame "SummaryScreen." Since you
don't want to show the quiz screen except the background at this point
of the game, you might want to do Clear Frames to clean the
screen and get rid off the buttons.
Let's compose the screen. Make a key
frame on the Animations layer. For this
example, I'll just put up some message and several textboxes.
The good news is that we already have most of the values
that we need here. For the Correct Answer box, we can just display
the value of the variable numOfQuestionsAnsweredCorrectly; and
for the Incorrect Answer box, we can just display the value of the
variable numOfQuestionsAnsweredIncorrectly. If you recall,
we have been updating these variables regularly. So, in the
property dialog for placeholder1, set Var: numOfQuestionsAnsweredCorrectly,
and also make sure Dynamic Text is selected.
In the textbox property dialog for placeholder2,
make sure to select Dynamic Text, and to set Var: numOfQuestionsAnsweredIncorrectly.
Now, for the score, we will need to do some
calculation. We'll do the calculation in a bit later, for now,
let's decide a variable name to use. Let's call the variable: userScore.
So, in the textbox property dialog for placeholder3, make
sure to select Dynamic Text, and to set Var: userScore.
Let's calculate the value of userScore.
We'll calculate it in the Actions layer of the "ShowScreen"
frame. You can use any kind of scoring you want, but for this
example, I will be dividing the number of correct answers by the total
answers. And then, I'll multiply the result by 100. So, the
range of score is from 0 to 100. The code is as follows:
userScore=(numOfQuestionsAnsweredCorrectly*100)/
(numOfQuestionsAnsweredIncorrectly+numOfQuestionsAnsweredCorrectly)
|
It looks like this in the movie:
Notice that I put a stop() action
to make the movie stop at this point. The Play Again button
can be set to restart the quiz by attaching this code:
We are done for now!
Test the movie.
Download
FLA