11.12 The Multiple-Choice Quiz, Take 3

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 11.  Arrays

In Example 9-13, we revised the multiple-choice quiz from Chapter 1 (Examples Example 1-1 through Example 1-4). At that time, we improved the quiz code by creating reusable, centralized functions. Now that we've studied arrays, we can return to the quiz to make a few more improvements. This time, we'll use arrays to store both the user's answers and the correct answers for each question, making our code more succinct, legible, and easier to extend if we want to add new questions.

The changes we need to make to the quiz take place on the scripts layer of frame 1, where our main quiz code is located. Both the previous version and this updated version of the quiz can be found in the online Code Depot. To see what improvements we'll make this time around, read through the code in Example 11-7, paying special attention to the comments.

Example 11-7. A multiple-choice quiz, version 3
//  Stop the movie at the first question. stop( );     // =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  = // INIT QUIZ VARIABLES // =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  = // Store a reference to the timeline that contains the quiz user interface. var quizTimeline = this;         // Create an array to contain user's answers.                           var userAnswers = new Array( );  // Create an array to contain each question's correct answer. var correctAnswers = [3, 2]; // Convenience variable to store number of questions in quiz. var numQuestions = correctAnswers.length;     // =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  = // CREATE QUIZ FUNCTIONS // =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  = //   Function: answer( ) //       Desc: Registers the user's answers to quiz questions and //             advances the quiz through its questions. //     Params: choice           The user's answer for the current question. function answer (choice) {   // Add the current answer to the list of user answers.   userAnswers.push(choice);       // Convenience variable to store number of current question.   var currentQuestion = userAnswers.length;       // Display the question and answer in the Output window for debugging.   trace("Question " + currentQuestion           + ". The user answered: " + userAnswers[currentQuestion-1] + ".");       // If we're on the last question...   if (currentQuestion =  = numQuestions) {     // ...go to the quiz end frame.     quizTimeline.gotoAndStop("quizEnd");     // And grade the user.     gradeUser( );   } else {     // ...otherwise, go to the next question frame.     quizTimeline.gotoAndStop("q"+ (currentQuestion + 1));   } }     //   Function: gradeUser( ) //       Desc: Tallies the user's score at the end of the quiz. function gradeUser ( ) {   // Report that we're about to grade the quiz in the Output window.   trace("Quiz complete. Now grading...");       // Create a local variable to track the    // number of questions the user answered correctly.   var totalCorrect = 0;             // Count how many questions the user answered correctly.   // For each question...   for (var i=0; i < numQuestions; i++) {     // If the user's answer matches the correct answer.     if (userAnswers[i] =  = correctAnswers[i]) {       // Give the user a point.       totalCorrect++;     }     // Display the correct answer and the user's answer     // in the Output window for debugging.     trace("Question " + (i + 1)           + ". Correct answer: " + correctAnswers[i]          + ". User's answer: "  + userAnswers[i]);   }       // Display the final score in the Output window for debugging.   trace("User's score: " + totalCorrect + "/" + numQuestions);       // Create an onscreen text field to display the user's score.   quizTimeline.createTextField("totalOutput_txt", 1, 150, 200, 200, 20);       // Show the user's score in an onscreen text field.   quizTimeline.totalOutput_txt.text = "Your final score is: "                                      + totalCorrect + "/" + numQuestions;       // Customize the font face, size, and color of the text field.   var formatObj = new TextFormat( );   formatObj.size = 16;   formatObj.color = 0xC7FF9C;   formatObj.font = "_sans";   formatObj.bold = true;   quizTimeline.totalOutput_txt.setTextFormat(formatObj); }     // =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  = // DEFINE QUIZ BUTTON EVENT HANDLERS // =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  = // Code executed when button 1 is pressed. choice1_btn.onRelease = function ( ) {   // Call answer( ), which records the user's choice   // and advances the quiz to the next question.   this._parent.answer(1); };     // Code executed when button 2 is pressed. choice2_btn.onRelease = function ( ) {   this._parent.answer(2); };     // Code executed when button 3 is pressed. choice3_btn.onRelease = function ( ) {   this._parent.answer(3); }; 
     



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net