But how exactly does the condition q1answer == 3
work? Well, let's break the phrase down. We recognize
q1answer as the variable in which we've
stored the user's answer to Question 1. The number 3 indicates
the correct answer to Question 1, because movie clips first appeared
in Flash 3. The double equal sign (==) between our variable and the
number 3 is the equality comparison operator,
which compares two expressions. If the expression on its left
(q1answer) equals the one on its right (3), our
condition is met, and the statements within the curly braces are
executed. If not, our condition is not met, and the statements within
the curly braces are skipped.
Flash has no way of knowing the right answers to our quiz questions.
Checking if q1answer is equal to 3 is our way of
telling Flash to check if the user got Question 1 right. If he did,
we tell Flash to add one to his total score as follows:
totalCorrect = totalCorrect + 1;
Line 3 says, "Make the new value of
totalCorrect equal to the old value of
totalCorrect plus one," (i.e.,
increment totalCorrect).
Incrementing a variable is so common that it has its own special
operator, ++.
So instead of using this code:
totalCorrect = totalCorrect + 1;
We normally write:
totalCorrect++;
which does exactly the same thing, but more succinctly.
At line 4, we end the block of statements to execute if our first
condition is met:
}
Lines 5 through 7 are another condition:
if (q2answer == 2){
totalCorrect++;
}
Here we're checking whether the user answered Question 2
correctly (MP3 audio support first appeared in Flash 4). If the user
chose the second answer, we add one to
totalCorrect using the increment operator ++.
Because there are only two questions in our quiz, we're done
tallying the user's score. For each question that the user
answered correctly, we added one to totalCorrect,
so totalCorrect now contains the user's
final score. The only thing left is to show the user his score, via
line 9, the last line of our quiz-end script:
displayTotal = totalCorrect;