After I wrote the article about creating a Quiz program, I got comments from people requesting that it be adapted to use an external XML file. In this article, we will modify it so that it does exactly that. So for those of you who requested it, this ought to satisfy both of you
.
Note: The starter files are available for download on the first page of this article. The finished files are available for download from the bottom of the last page.
xml file
hi! sir
i have seen your tutorial on quiz xml
well but i want the question to be displayed in the randomize flow
so that the question should be displayed randomly 4m xml file
if you can please tell me the code of randomize functin to xml content file
Quiz Part 2 Question
Mr. Hall,
I am using the quizApp function with xml as part of a school project, as advised by my professor. However, he cannot answer the question that is plaguing me. My application has a black background and I have been able to change the text color within quizQuestion.as from black to white. However, the answers still show black and are not viewable. Where can I change this color? I'm completely lost and have numerous attempts with it without success. Any help would be most appreciated. Here is the link to my school page so you may see what I mean:
http://ist2w.kaplan.edu/1103C/IT490-01/RobertBeddow2/
Sincerely,
Robert
Changing the text colors
Robert,
Apparently you got so far as to change the color of the question, but I'll go over that, too, as I just did it.
Open the QuizQuestion.as file. In the class's constructor (the public function with the same name as the class) add these lines to the top of the function (insert them ahead of everything else that's there):
//create the textformats for the question and answers:
var questionFormat = new TextFormat("Arial", 16);
questionFormat.color = 0xFFFFFF;
questionFormat.bold = true;
var answerFormat = new TextFormat("Arial", 12);
answerFormat.color = 0xFFFFFF;
Make sure that the TextFormat class is being imported, too, so you must add this line to your list of imports:
import flash.text.TextFormat;
Next, just before the line that sets the text property of the question, add this line:
questionField.defaultTextFormat = questionFormat;
Next, setting the text format for the answers is done differently, because the answers are all radio button instances. The way that you do this is like so (put this line in the for loop, just before the line that sets the label (text) for the radio buttons. The preceding line says:
rb.label = choices[i];
Anyway, just above that line, insert this line:
rb.setStyle("textFormat", answerFormat);
And that will do it for you. There is one other issue concerning the question of whether to embed the font or not. If you don't embed the font, you are relying on the user's system to supply the font, and if the font is not available, one will be substituted. But if you use a common font like I did above (Arial), it probably won't be a problem, and it keeps your swf's file size down. But if you want to use an exotic font, you will probably want to embed it. That's a whole 'nother question, though, and I will spare myself answering it unless you really need it.
Error Message
Mr. Hall,
After placing in the code, I received the following error:
TypeError: Error #1090: XML parser failure: element is malformed.
at QuizApp1/xmlLoaded()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
Here is the code I used in the QuizQuestion.as:
package {
import flash.display.MovieClip;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.events.Event;
import fl.controls.RadioButton;
import fl.controls.RadioButtonGroup;
public class QuizQuestion extends MovieClip {
private var question:String;
private var questionField:TextField;
private var choices:Array;
private var theCorrectAnswer:int;
private var theUserAnswer:int;
//variables for positioning:
private var questionX:int = 100;
private var questionY:int = 100;
private var answerX:int = 160;
private var answerY:int = 155;
private var spacing:int = 25;
public function QuizQuestion(snippet:XML) {
//create the textformats for the question and answers:
var questionFormat = new TextFormat("Arial", 16);
questionFormat.color = 0xFFFFFF;
questionFormat.bold = true;
var answerFormat = new TextFormat("Arial", 12);
answerFormat.color = 0xFFFFFF;
//store the supplied arguments in the private variables:
question = snippet.question;
theCorrectAnswer = snippet.answer;
choices = new Array();
for each(var string:String in snippet.choice) {
choices.push(string);
}
//create and position the textfield (question):
questionField.defaultTextFormat = questionFormat;
questionField = new TextField();
questionField.text = question;
questionField.autoSize = TextFieldAutoSize.LEFT;
questionField.x = questionX;
questionField.y = questionY;
addChild(questionField);
//create and position the radio buttons (answers):
var myGroup:RadioButtonGroup = new RadioButtonGroup("group1");
myGroup.addEventListener(Event.CHANGE, changeHandler);
for(var i:int = 0; i < choices.length; i++) {
var rb:RadioButton = new RadioButton();
rb.textField.autoSize = TextFieldAutoSize.LEFT;
rb.setStyle("textFormat", answerFormat);
rb.label = choices[i];
rb.group = myGroup;
rb.value = i + 1;
rb.x = answerX;
rb.y = answerY + (i * spacing);
addChild(rb);
}
}
private function changeHandler(event:Event) {
theUserAnswer = event.target.selectedData;
}
public function get correctAnswer():int {
return theCorrectAnswer;
}
public function get userAnswer():int {
return theUserAnswer;
}
}
}
I'm not sure what went wrong, but I'd be happy to send you the entire file to review. Thanks in advance for any possible assistance.
Sincerely,
Robert
I'll check it out for you
Robert,
The error message seems to be saying that there is something wrong with the XML file.
You can zip up the entire project and attach it to an email to: mazoonist *at* gmail *dot* com. I'd be happy to take a look at it.
Jody
I appreciate it
Mr. Hall,
I sent the package out to you. Thanks again in advance.
-Robert