Click to See Complete Forum and Search --> : Trouble w/ Basic Flash quiz using XML data file


Warfman99
09-04-2008, 10:38 AM
I'm trying to mimic this simple flash quiz I found online:
http://www.permadi.com/tutorial/flashMXQuiz/index.html

It works fine when I open the .swf file by itself in a browser and take the quiz. However, when I embed the file on a webpage, the quiz goes directly to the last screen, as if it isn't pulling any of the data (all the questions) from the .xml file.

I've triple-checked the filename is correct for the .xml file. The .swf and .xml files are in the same directory. Let me know if you need any more info that you think might be helpful in figuring out the issue.

Any help is greatly appreciated.
Thanks.

HTML CODE TO EMBED SWF ON WEBPAGE:
<object style="z-index:1;" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="576" height="357" title="Smile &amp; Move&trade; Teens Quiz">
<param name="movie" value="/flash/quiz/quiz.swf" />
<param name="quality" value="high" />
<param name="wmode" value="transparent">
<embed src="/flash/quiz/quiz.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="576" height="357"></embed>
</object>

ACTIONSCRIPT THAT CALLS THE .XML FILE:
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");
}
}

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);
var a=1;
// <answer> follows <question>
var answerNode=itemNode.childNodes[a++];
while (answerNode)
{
var isCorrectAnswer=false;
if (answerNode.attributes.correct=="y")
isCorrectAnswer=true;
quizItems[i].addAnswer(answerNode.firstChild.nodeValue, isCorrectAnswer);
// goto the next <answer>
answerNode=itemNode.childNodes[a++];
}
i++;
}
gotoAndPlay(_currentFrame+1);
}

var quizItems=new Array();
var myData=new XML();
myData.ignoreWhite=true;
myData.onLoad=onQuizData;
myData.load("quiz.xml");
stop();

WPeterson
10-07-2008, 01:31 AM
Usually kinda problems w/ the file path. maybe all files including .swf, .xml and .html should be in the same directory, I guess.

Besides, I realize that the best idea of any Flash quizzes is the flash shell and xml data file combined in one Flash file. And Adobe Flash CS3 already has some quiz templates ready, if you'd like to make Flash quiz from Flash, go this one-file way without external data file reading.

Finally, I recommend some easy-to-use quiz tools such as QuizCreator (http://www.sameshow.com/quiz-creator.html) to simplify the way without coding.

William Peterson

Eye for Video
10-07-2008, 10:53 AM
Pathing can sometimes be an issue. Just remember that the path to your .swf and .xml changes depending on the location of the page calling the file. For example, in your code you list the path to the .swf as:
<param name="movie" value="/flash/quiz/quiz.swf" />
yet you show the path to the .xml file as:
myData.load("quiz.xml");
After being loaded into the html page, the .swf and the .xml are no longer in the same folder. So the path from the new location on the html page would be something like:
myData.load("/flash/quiz/quiz.xml");
For a more lengthy explaination on pathing .swf and .xml, read my last post on this topic:
http://www.webdeveloper.com/forum/showthread.php?t=188073
Best wishes,
Eye for Video
www.cidigitalmedia.com