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 a sample.
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 at the end.
stop();
var totalCorrect=0;
choice1.onRelease = function () {
this._parent.q1answer = 1;
this._parent.gotoAndStop ("q2");
}
choice2.onRelease = function () {
this._parent.q1answer = 2;
this._parent.gotoAndStop ("q2");
}
choice3.onRelease = function () {
this._parent.q1answer = 3;
this._parent.gotoAndStop ("q2");
}
You should know what stop(); does. The next line resets the total number of correct answers to zero (especially important if you want people to try the quiz again). Then the script basicaly says "if the user chooses "choice 1," then set the value of variable "g1answer" to 1; after that go to frame q2. If the user chooses choice 2...." etc.
choice1.onRelease = function () {
this._parent.q2answer = 1;
this._parent.gotoAndStop ("q3");
}
choice2.onRelease = function () {
this._parent.q2answer = 2;
this._parent.gotoAndStop ("q3");
}
choice3.onRelease = function () {
this._parent.q2answer = 3;
this._parent.gotoAndStop ("q3");
}
Notice the code is nearly identical to the code in step 3 above, except we'ver dropped the stop; (not needed), and var totalCorrect=0; (otherwise the user's score will always remain zero). We've also changed "q1answer" to "q2answer" in all three statements and the gotoAndStops now go to "q3."
if (q1answer==1) {
totalCorrect = totalCorrect +1;
}
if (q2answer==1) {
totalCorrect=totalCorrect +1;
}
if (q3answer==1) {
totalCorrect=totalCorrect +1;
}
if (q4answer==1) {
totalCorrect=totalCorrect +1;
}
if (q5answer==1) {
totalCorrect=totalCorrect +1;
}
if (q6answer==1) {
totalCorrect=totalCorrect +1;
}
if (q7answer==1) {
totalCorrect=totalCorrect +1;
}
if (q8answer==1) {
totalCorrect=totalCorrect +1;
}
if (q9answer==1) {
totalCorrect=totalCorrect +1;
}
if (q10answer==1) {
totalCorrect=totalCorrect +1;
}
this.createTextField("totalOutput_txt", 1, 250, 150, 250, 20);
totalOutput_txt.text="Your Final Score is: " + totalCorrect + " out of 12";
var formatObj = new TextFormat();
formatObj.size = 16;
formatObj.color = 0xFFFFFF;
formatObj.font = "Arial";
totalOutput_txt.setTextFormat(formatObj);
}
Here are a series of "if" statements. Each one checks the value of the answers selected against the actual correct answers. If they are the same then the "totalCorrect" increases by one.
Very important: in this example all of the correct answers are choice 1 (for example, q1answer==1). Obviously, you'll need to change the answers for your quiz.
This 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.
The code presented here works, but it's kinda' clunky; it's spread out over multiple frames. I also have a version of this same exercise that is more elegant—all of the code for the entire quiz is contained within one script in one frame! What's that catch? It's a lot harder to understand for a novice; it makes use of more advanced techniques that might cause you to scratch your head and say "huh?" Check it out and try it out, see what you think...
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. |