PDA

View Full Version : xml quiz


fourteen
12-19-2005, 02:27 PM
Hi all

The code below is for a quiz im working on - Im trying to display the note and backgroundImg for every individual question, but cannot work out how to do so, the notes arent displayed individually and the background wont change as you cycle through the questions...im new to incorporating xml into flash and therefore not sure if theres a better way to populate attributes from within the xml file - any help would be greatly appreciated thanks...

//Create new Array
var quizItems=new Array();

//Load XML data
var myData=new XML();
myData.ignoreWhite=true;
myData.onLoad=onQuizData;
myData.load("xml/quiz.xml");

function QuizItem(question)
{
this.question=question;
this.answers=new Array();
this.numOfAnswers=0;
this.correctAnswer=0;
this.getQuestion=function()
{
return this.question;
}
this.addAnswer=function(answer, isCorrectAnswer)
{
this.answers[this.numOfAnswers]=answer;
if (isCorrectAnswer)
this.correctAnswer=this.numOfAnswers;
this.numOfAnswers++;
}

this.getAnswer=function(answerNumberToGet)
{
return this.answers[answerNumberToGet];
}

this.getCorrectAnswerNumber=function()
{
return this.correctAnswer;
}

this.checkAnswerNumber=function(userAnswerNumber)
{
if (userAnswerNumber==this.getCorrectAnswerNumber()){
gotoAndPlay("Correct");
}
else{
gotoAndPlay("Wrong");
//Display correct answer when user is wrong
var rightAns=this.correctAnswer;
if (rightAns==0){
rightAns="A";
}
else if(rightAns==1){
rightAns="B";
}
else if(rightAns==2){
rightAns="C";
}
else if(rightAns==3){
rightAns="D";
}
correctAnswer_txt=rightAns;
}
}

this.getNumOfAnswers=function()
{
return this.answers.length;
}
}

function onQuizData(success)
{
var quizNode=this.firstChild;
var quizTitleNode=quizNode.firstChild;
title=quizTitleNode.firstChild.nodeValue;

var i=0;
// <items> follows <title>
var itemsNode=quizNode.childNodes[1];
while (itemsNode.childNodes[i])
{
var itemNode=itemsNode.childNodes[i];
// <item> consists of <question> and one or more <answer>
// <question> always comes before <answer>s (node 0 of <item>)
var questionNode=itemNode.childNodes[0];
quizItems[i]=new QuizItem(questionNode.firstChild.nodeValue);

//Background
var bgImg=questionNode.attributes.backgroundImg;
_root.createEmptyMovieClip("mcImageHolder",-1234567);
mcImageHolder._x = 0;
mcImageHolder._y = 0;
mcImageHolder.loadMovie(bgImg);

//note_txt is the dynamic text field for the notes
note_txt=answerNode.attributes.note;

var a=1;
// <answer> follows <question>
var answerNode=itemNode.childNodes[a++];

while (answerNode)
{
var isCorrectAnswer=false;
//which answer is correct?
if (answerNode.attributes.correct=="y")
isCorrectAnswer=true;

quizItems[i].addAnswer(answerNode.firstChild.nodeValue, isCorrectAnswer);

// goto the next <answer>
answerNode=itemNode.childNodes[a++];
}
i++;
}
gotoAndStop("Start");
}

XML Code

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE quiz [
<!ELEMENT quiz (title, items)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT items (item)+>
<!ELEMENT item (question, answer, answer+)>
<!ELEMENT question (#PCDATA)>
<!ATTLIST question backgroundImg bgimg #REQUIRED>
<!ELEMENT answer (#PCDATA)>
<!ATTLIST answer correct (y) #IMPLIED>
<!ATTLIST answer img (#PCDATA)>
]>

<quiz>
<title>Test Quiz</title>
<items>
<item>
<question backgroundImg="background/Fire.jpg">In what year was the sale of tobacco to under 16's banned?.</question>
<answer>1808</answer>
<answer>1888</answer>
<answer note="This is a note" correct="y">1908</answer>
<answer>1988</answer>
</item>
<item>
<question backgroundImg="background/AlcoholBkg2.jpg">On average, how many minutes of life are lost for every cigarette smoked?</question>
<answer note="this is a note 2">3</answer>
<answer correct="y">5</answer>
<answer>10</answer>
<answer>15</answer>
</item>
<item>
</items>
</quiz>

fourteen
12-20-2005, 12:49 PM
to put it a different way:

how do you interact with the XML (e.g. itemNode.childNodes.length) from a frame outside of the first frame (where the XML load takes place), do I need to create a function declaring the value for each different interaction?

thanks

finchou
12-20-2005, 01:11 PM
you can do your xml loading in an empty frame that is as long as the movie, this way you can access _level0.myData from anywhere...
or you can set it as a _global variable...

fourteen
12-20-2005, 02:08 PM
thanks very much for your reply finchou i hadnt come across that method before