Listing 4. Quiz Program with More Questions

<?xml version="1.0" encoding="utf-8"?>
<!--Example for LJ article. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                layout="vertical"
                backgroundColor="#FFFFFF">
  <mx:TextArea id="Question"
               width="100%" height="15%"
               paddingTop="10" paddingBottom="10"
               paddingLeft="10" paddingRight="10"
               editable="false" backgroundColor="#FFFFFF"
               borderColor="#FFFFFF"
               creationComplete="initApp();">
    <mx:htmlText>
    <![CDATA[
      <font size="+4" face="Arial">
        <b>
          Question 1: Which strip is this grouchy but
                      good-hearted fighter the star of?
        </b>
      </font>
    ]]>
    </mx:htmlText>
  </mx:TextArea>
  <mx:VBox paddingLeft="150"
           backgroundColor="#FFFFFF" width="100%">
    <mx:RadioButtonGroup id="Answers" />

    <mx:RadioButton id="a1" groupName="Answers" value="1"
                    label="Belkar Bitterleaf"
                    width="400" paddingRight="20" />
    <mx:RadioButton id="a2" groupName="Answers" value="2"
                    label="Gilgamesh Wulfenbach"
                    width="400" paddingRight="20" />
    <mx:RadioButton id="a3" groupName="Answers" value="3"
                    label="Roy Greenhilt"
                    width="400" paddingRight="20" />
    <mx:RadioButton id="a4" groupName="Answers" value="4"
                    label="Frank Mangle"
                    width="400" paddingRight="20" />
    <mx:Button id="nextButton"
               label="Next" click="parseanswers();" />
  </mx:VBox>
  <mx:Script>
  <![CDATA[
  import mx.controls.Alert;
  // Define here to make variables global.
  var currentQuestion:int = 0;
  var totalRight:int = 0;
  // Initialize the array of questions.
  //   - First sub-array is question text
  //   - Second through fifth are possible answers
  //   - Sixth is the correct answer for that question.
  var questions:Array = new Array(
      new Array("This grouchy but ... Order of the Stick.",
                "In Kevin and Kell, why ... secretaries?",
                "Which of these strips is NOT a stick-figure comic?"),
      new Array("Belkar Bitterleaf",
                "High metabolism means lots of work done.",
                "Questionable Content"),
      new Array("Gilgamesh Wulfenback",
                "Hollow bones mean no carpal tunnel",
                "No Time for Life"),
      new Array("Roy Greenhilt",
                "Excellent language skills of parrots",
                "xkcd"),
      new Array("Frank Mangle",
                "Flight lets them double as messengers",
                "Cyanide and Happiness"),
      new Array(3,2,1));

  private function initApp(): void
  // Initializes the first question, removing the default text
  // that's there when the controls are created
  {
      // testing
      Question.text=questions[0][currentQuestion];
      a1.label=questions[1][currentQuestion];
      a2.label=questions[2][currentQuestion];
      a3.label=questions[3][currentQuestion];
      a4.label=questions[4][currentQuestion];
  }
  public function parseanswers():void
  // Function runs whenever the user clicks the Next button.
  // Updates the score and puts up the new question, except on
  // the final question, where it displays the user's final tally.
  {
      if (Answers.selectedValue == questions[5][currentQuestion]) {
          totalRight++
      }
      // OK, Next was pressed so it's time to update the
      // screen. Test whether this was the last question,
      if (currentQuestion >= 2) {
          // That was the last question, time to report
          // results. For simplicity I will use
          // the Alert function here
          Alert.show('You got '+ totalRight
                     + ' questions right out of '
                     + 3, 'Score', mx.controls.Alert.OK);
          // Since the quiz is over, I disable all the
          // controls on the screen. In a polished
          // version, I will handle the end of quiz by switching
          // to a different Flash file showing the score only.
          Question.enabled=false;
          Answers.enabled=false;
          nextButton.enabled=false;
      }
      else {
          // OK, that wasn't the last question, so update
          // the question, all four answers and
          // increment currentQuestion
          currentQuestion++;
          Question.text=questions[0][currentQuestion];
          a1.label=questions[1][currentQuestion];
          a2.label=questions[2][currentQuestion];
          a3.label=questions[3][currentQuestion];
          a4.label=questions[4][currentQuestion];
          Answers.selection = null;
      }
  }
  ]]>
  </mx:Script>
</mx:Application>

