Note: this a a more elegant (but trickier to understand) version of the quiz exercise found here.
Create a simple quiz on a topic of your choosing in Flash. The quiz should count the number of correct answers and return a score to the user. Here is an example.
Now it would be a simple matter to put a script on each button that gives you instant feedback as to whether on not the answer is correct, but we're going to be a bit more tricky. We're going to collect the answers, tabulate them and then give the user a final score. Better still, we're going to do all of this with just one script (rather than one on each button or in each frame).
stop();
var quizTimeline = this;
var numQuestions = 10;
var q1answer;
var q2answer;
var q3answer;
var q4answer;
var q5answer;
var q6answer;
var q7answer;
var q8answer;
var q9answer;
var q10answer;
You should know what stop(); does. The rest of the items set variables. The first one creates a variable called "quizTimeline" that refers to the main timeline of the movie. The second variable sets the number of questions (if you want to add more questions adjust accordingly). The next 10 lines create a set of variables we will use later.
var correctAnswer1 = 1;
var correctAnswer2 = 1;
var correctAnswer3 = 1;
var correctAnswer4 = 1;
var correctAnswer5 = 1;
var correctAnswer6 = 1;
var correctAnswer7 = 1;
var correctAnswer8 = 1;
var correctAnswer9 = 1;
var correctAnswer10 = 1;
For each question in your quiz you will have to change the value of the corrsponding variable above. For example, if the answer to your first question is the third option, the first line should read var correctAnswer1 = 3; and so on.
answer.currentQuestion = 0;
function answer(choice) {
answer.currentQuestion++;
set("q"+ answer.currentQuestion+"answer", choice);
if (answer.currentQuestion == numQuestions) {
quizTimeline.gotoAndStop("end");
gradeUser();
} else {
quizTimeline.gotoAndStop("q"+(answer.currentQuestion+1));
}
}
Okay this is where things get complicated. First the value to the answer.currentQuestion is set to 0. Then, a function is created that does several things: it adds 1 to answer.currentQuestion; it figures out which button was pressed and assigns the value to the variable q1answer (the values of the buttons is set further down); and it looks to see if the final question was answered (if so, it jumps to the end of the timeline, otherwise it adds 1 to the currentQuestion and proceeds to the next question inthe timeline). Whew! That's a lot packed into a small space. But it's very efficient.
function gradeUser() {
var totalCorrect = 0;
for (var i = 1; i<=numQuestions; i++) {
if (eval("q"+i+"answer") == eval("correctAnswer"+i)) {
totalCorrect++;
}
}
quizTimeline.createTextField("totalOutput_txt", 1, 260, 150, 250, 20);
quizTimeline.totalOutput_txt.text = "Your final score is: "+totalCorrect+"/"+numQuestions;
var formatObj = new TextFormat();
formatObj.size = 18;
formatObj.color = 0xFFFFFF;
formatObj.font = "Arial";
formatObj.bold = true;
quizTimeline.totalOutput_txt.setTextFormat(formatObj);
}
This function (gradeUser) kicks in at the end of the timeline. For each question it compares the value of the answer given (q1answer, for example) with the value of the correct answer (correctAnswer1, for example). If they are the same, the totalCorrect increases by one.
But the function then does something else: it creates a text field (the four numbers are its x & y position followed by width and height), and then writes the results of the quiz in that text field. The formatObj items set font family, size, color, etc.
choice1.onRelease = function () {
this._parent.answer(1);
}
choice2.onRelease = function () {
this._parent.answer(2);
}
choice3.onRelease = function () {
this._parent.answer(3);
}
These three lines simply read which of the buttons was pressed (choice1, choice2, and choice3 are the button instance names, remember?) and pass that information along to the variable answer that we use above.
4 pts |
Portfolio quality design; great quiz questions. |
3 pts |
Good looking; above average work. |
2 pts |
It works, but it's nothing fancy. |
1 pts |
Something's not working right. |
0 pts |
Poor showing; mostly incomplete or full of errors. |