Quiz XML Data
Quiz XML Data
<Quiz>
</Quiz>
<Quiz>
<Question>
</Question>
</Quiz>
Here we have added one question to our XML file. You can add more questions but for now it
will work. Later we shall add good set of questions.
3) A quiz is incomplete without options with one right answer to choose from.
<Quiz>
<Question>
</Question>
</Quiz>
Here we set a question with three options. For now we shall use this XML.
3) Add dynamic text field (Arial, 20, bold) to the stage and place it at the top. Expand it enough
as it is going to display your question. Give Instance name as Question_TF.
xmlLoader.addEventListener(Event.COMPLETE, onXMLLoad);
var xml:XML;
var xmlList:XMLList;
var xmlChildren;
function onXMLLoad(e:Event):void
Call this function inside onXMLLoad function as shown below. (Bold line)
function onXMLLoad(e:Event):void {
Question_TF.text = xmlChildren.QText;
var totalQuestions:Number;
Question_TF.text = xmlChildren.QText;
totalQuestions = xmlChildren.length();
(Note: For CS5 embed font for action script using properties panel for text field)
There it is! Our first question is nicely placed in the text field.
To achieve this we need total number of options specified in XML file. So that we can decide
how many radio buttons needs to be added to the stage below a Question.
1) Get total number of options for the current question as follows, (Bold lines)
var totalOptions: Number;
totalOptions = xmlChildren.Option.length();
function generateQuestion():void{
Question_TF.text = xmlChildren.QText;
totalQuestions = xmlChildren.length();
generateOptions();
2) Before creating and placing radio buttons we must have to do couple of things.
import fl.controls.RadioButton;
import fl.controls.RadioButtonGroup;
Or
(Drag it to the stage. So that it is available in the library. But here delete it from the stage after
dragging because we are going to add them dynamically.)
3) To create and place radio buttons modify generateOptions() function and add new vars to the
script as follows, (Bold lines)
var totalOptions:Number;
var radioButton:RadioButton;//a radio button
function generateOptions():void{
totalOptions = xmlChildren.Option.length();
vPadding += vSpacing;
But they are required to be filled with options specified in xml file. Perform next step.
vPadding += vSpacing;
radioButton.label = xmlChildren.Option[i];
COOL!!! All radio buttons are displaying options extracted from xml file.
Any Guess?
This is the perfect situation where we can take the help of XML attribute.
<Quiz>
<Question>
</Question>
</Quiz>
We have added XML attribute correct = 1. We shall use this attribute to set current option as a
correct answer.
a) Get an index number of the option (XML file) having attribute correct = 1.
c) Compare these index numbers with each other. If they are equal, the answer
is correct else it is incorrect.
2) So we have a new task now. Get the index number of the option having attribute correct = 1
in xml file. So modify the script as follows. (Bold lines)
var totalOptions:Number;
var radioButton:RadioButton;
var vPadding:uint = 0;
var isCorrect:Number;
var correctAnswerIndex:Number;
function generateOptions():void{
totalOptions = xmlChildren.Option.length();
addChild(radioButton);
vPadding += vSpacing;
radioButton.label = xmlChildren.Option[i];
radioButton.width = 300;
isCorrect = xmlChildren.Option[i].@correct;
if(isCorrect == 1){
Our next target is to get an index number of selected radio button. But wait. It is not as straight
forward as you think.
Before that let us create a group of radio buttons as follows (Bold lines);
var totalOptions:Number;
var radioButton:RadioButton;
var radioButtonGroup:RadioButtonGroup;
var vPadding:uint = 0;
var isCorrect:Number;
var correctAnswerIndex:Number;
function generateOptions():void {
totalOptions = xmlChildren.Option.length();
addChild(radioButton);
vPadding += vSpacing;
radioButton.label = xmlChildren.Option[i];
radioButton.width = 300;
isCorrect = xmlChildren.Option[i].@correct;
if(isCorrect == 1){
correctAnswerIndex = i;
This step will not only satisfy our second need of getting an index number of selected radio
button but we are also adding a new function checkAnswer() to the script as follows,
var currentRadioButton;
var currentSelection;
function checkAnswer(e:MouseEvent):void {
currentSelection = radioButtonGroup.getRadioButtonIndex(currentRadioButton));
It is time to compare them with each other. Follow the next step
We shall modify the checkAnswer (e: MouseEvent) function as shown, (Bold lines)
var currentRadioButton;
var currentSelection:Number;
var resultStr:String;
var result_TF:TextField = new TextField (); //to display result for current question
function checkAnswer(e:MouseEvent):void {
currentSelection = (radioButtonGroup.getRadioButtonIndex(currentRadioButton));
//Check and store answer
//Display a result
result_TF.text = resultStr;
If you test the movie and click radio buttons nothing will happen. Why? Follow next step.
vPadding += vSpacing;
//Extract option from XML file and add it to radio button label
radioButton.label = xmlChildren.Option[i];
if(isCorrect == 1){
radioButton.addEventListener(MouseEvent.CLICK, checkAnswer);
Congrats!!! They are responding showing result. Now we are very close to the final version.
<Quiz>
<Question>
</Question>
<Question>
</Question>
<Question>
</Question>
</Quiz>
I had intentionally placed different number of options for different questions. You can have your
choice. (of number of questions and their number of options). Only take care of placing attribute
correct = 1.
This was about XML. What about action script in Flash? We must modify it.
We shall utilize this var to point out (XML) index number of a question and so the related
options going to be displayed on the stage.
This will hold all radio buttons of current question. And will help to remove those radio buttons
from the stage and add new ones.
4) I know you are waiting for finishing touch. Please have patience. Face it.
This is an important member of our family. It will open the door to meet the next question and its
option.
Some part of the script is self explanatory. More focus on a logical part of it.
Dont test the movie now. Do it after going through next step as we are also going to modify our
generateOptions() function by adding radioButtonArray.
// Go to next question
function nextQuestion(e:MouseEvent):void{
Question_TF.text = ;
result_TF.text = ;
radioButtonContainer.removeChild(radioButtonArray[i]);
currentQuestion++;
vPadding = 0;
totalOptions = 0;
radioButtonArray = [];
generateQuestion();
var totalOptions:Number;
var radioButton:RadioButton;
var radioButtonGroup:RadioButtonGroup;
var vPadding:uint = 0;
var isCorrect:Number;
var correctAnswerIndex:Number;
var radioButtonArray:Array;
addChild(radioButtonContainer);
function generateOptions():void{
totalOptions = xmlChildren[currentQuestion].Option.length();
radioButton.selected = false;
radioButtonContainer.addChild(radioButton);
vPadding += vSpacing;
radioButton.label = xmlChildren[currentQuestion].Option[i];
radioButton.width = stage.stageWidth;
radioButton.group = radioButtonGroup;
isCorrect = xmlChildren[currentQuestion].Option[i].@correct;
if(isCorrect == 1){
correctAnswerIndex = i;
radioButton.addEventListener(MouseEvent.CLICK, checkAnswer);
radioButtonArray.push(radioButton);
}
function generateQuestion():void{
Question_TF.text = xmlChildren[currentQuestion].QText;
totalQuestions = xmlChildren.length();
generateOptions();
Finish next step and you are done with what you are expecting.
Give instance name as nextButton to your button. Dont worry about its position. We are going
to handle it in a script.
After creating a button our next task is to make it functioning. Modify function
checkAnswer(e:MouseEvent) as shown (Bold lines).
nextButton.visible = false;
var currentRadioButton;
var currentSelection:Number;
var resultStr:String;
result_TF.width = 250;
addChild (result_TF);
function checkAnswer(e:MouseEvent):void {
currentRadioButton = e.currentTarget;
currentSelection = (radioButtonGroup.getRadioButtonIndex(currentRadioButton));
result_TF.text = resultStr;
if(resultStr == Correct){
nextButton.visible = true;
nextButton.addEventListener(MouseEvent.CLICK,
nextQuestion);
nextButton.y = result_TF.y;
}else{
}else
nextButton.visible = false;
nextButton.removeEventListener(MouseEvent.CLICK,
nextQuestion);
}
This functions checks for correct answer. If answer is correct and the current question is not the
last question then it displays Next button on the stage. If answer is incorrect then it shows Quiz
Complete after result.
Good News!!!. Finally you can call it as XML driven quiz application.
Modify Quiz.xml as per your need and experience the power of XML Driven.
If you wish you can stop here or you perform next step. I suggest the later one.
We shall do some text formatting now. (Not great but will satisfy our need).
Our radio buttons labels are very poor. Make them more readable.
1) Add following lines at the start (below import statements) of the script
//Text formatting
globalTextFormat.font = Arial;
globalTextFormat.size = 18;
globalTextFormat.bold = true;
//
2) To apply the above textformat to radio button labels, add following line into for loop of
generateOptions() as shown (Bold line)
radioButton.setStyle(textFormat,globalTextFormat);
var currentRadioButton;
var currentSelection:Number;
var resultStr:String;
var resultStrColor;
result_TF.defaultTextFormat = globalTextFormat;
result_TF.width = 250;
addChild (result_TF);
function checkAnswer(e:MouseEvent):void {
currentRadioButton = e.currentTarget;
currentSelection = (radioButtonGroup.getRadioButtonIndex(currentRadioButton));
result_TF.text = resultStr;
//Change text color of result as per answer (Green for correct and Red for incorrect)
result_TF.textColor = resultStrColor;
}.
Nice Music Please!!! I am very much excited to say that we have finished our long journey to
reach to see how to build XML Driven Quiz Application.
Conclusion
After completing this Flash tutorial you are able to develop any XML driven application. We also
got opportunity to handle radio buttons. In the final section we did text formatting which will
certainly help in your future work.
Since our goal was to learn to build XML driven application we ignored beauty and styling of
GUI. We were more focused on logical part. We also ignored scoring for the same reason. Now
you know it is not that tough. I believe you can add it yourself. And you can improve it
yourself. Thanks for your patience. Have a nice time and best of luck. Come back to see my
more upcoming tutorials.