Im using MX2004 pro and I would like to know if there is a way to have a series of questions loaded in to to flash for a quiz. I have found a few tutorials on this, but have only seen it done with multiple txt files per question. I need to have it randomly pick a question from 1 text file so that it can be easily updated. Any ideas? Thanks!
This is a modification of a similar script I wrote a couple of days ago for someone who wanted to load a list of animals.
Put the following into a file called questions.xml, and put the file
the same place the .swf is.
Code:
<questionList>
<question q="What is furry and purrs?" a="A cat" />
<question q="What is furry and barks?" a="A dog" />
<question q="What is not furry" a="A shaved cat" />
</questionList>
Then put this actionscript on frame 1 of your movie. It loads the questions into an array of question/answer pairs.
Code:
// XML Question Loader - Jim Bumgardner
qList = new Array();
// This is a recursive function that
// walks thru all the nodes in your XML
// and loads up the ones you're
// interested in into the qList array.
loadNode = function(it)
{
if (it.nodeName == 'question') {
var qPair = new Array();
qPair.q = it.attributes['q'];
qPair.a = it.attributes['a'];
qList.push(qPair);
}
else if (it.nodeName == "questionList")
{
// process any questionList properties here
// - there aren't any at the moment.
// Note that the 'questionList' tag is
// completely ignored - this will still
// work without that tag.
}
if (it.hasChildNodes()) {
for (var i = 0; i < it.childNodes.length; ++i)
loadNode(it.childNodes[i]);
}
}
// when this function is called, we're done loading
// - do something with your data.
// Most likely you want to jump to a frame in your movie where you play the game, but I'll just output a few values
function finishInit()
{
trace(qList.length + " questions loaded:");
// Pick a random question
var r = random(qList.length);
trace("a random question: " + qList[r].q);
trace("...and its answer: " + qList[r].a);
}
var myxml = new XML();
myxml.onLoad = function()
{
// initialize array again, in case
// we're calling this twice...
qList = new Array();
loadNode(this);
finishInit();
}
myxml.load("questions.xml");
// A common mistake is to attempt to access the data here. Don't -- it is not loaded at this point
// myxml.load() is an asynchronous function - it returns before it is finished.
// The finishInit() function (above) will be called when the data is ready to go...
Originally posted by jbum This is a modification of a similar script I wrote a couple of days ago for someone who wanted to load a list of animals.
Put the following into a file called questions.xml, and put the file
the same place the .swf is.
Code:
<questionList>
<question q="What is furry and purrs?" a="A cat" />
<question q="What is furry and barks?" a="A dog" />
<question q="What is not furry" a="A shaved cat" />
</questionList>
Then put this actionscript on frame 1 of your movie. It loads the questions into an array of question/answer pairs.
Code:
// XML Question Loader - Jim Bumgardner
qList = new Array();
// This is a recursive function that
// walks thru all the nodes in your XML
// and loads up the ones you're
// interested in into the qList array.
loadNode = function(it)
{
if (it.nodeName == 'question') {
var qPair = new Array();
qPair.q = it.attributes['q'];
qPair.a = it.attributes['a'];
qList.push(qPair);
}
else if (it.nodeName == "questionList")
{
// process any questionList properties here
// - there aren't any at the moment.
// Note that the 'questionList' tag is
// completely ignored - this will still
// work without that tag.
}
if (it.hasChildNodes()) {
for (var i = 0; i < it.childNodes.length; ++i)
loadNode(it.childNodes[i]);
}
}
// when this function is called, we're done loading
// - do something with your data.
// Most likely you want to jump to a frame in your movie where you play the game, but I'll just output a few values
function finishInit()
{
trace(qList.length + " questions loaded:");
// Pick a random question
var r = random(qList.length);
trace("a random question: " + qList[r].q);
trace("...and its answer: " + qList[r].a);
}
var myxml = new XML();
myxml.onLoad = function()
{
// initialize array again, in case
// we're calling this twice...
qList = new Array();
loadNode(this);
finishInit();
}
myxml.load("questions.xml");
// A common mistake is to attempt to access the data here. Don't -- it is not loaded at this point
// myxml.load() is an asynchronous function - it returns before it is finished.
// The finishInit() function (above) will be called when the data is ready to go...
- jim
Thanks for that. how would I go about displaying all of this on different frames so that it takes the form of a quiz? It also needs to determine whether or not the correct answer was selected. Thanks!
I'm not up on the XML method although I think I will use this as a reference and try to learn it.
_t
The questions would need to have the answers in them, as well as what the correct answer is. Do you think you could make an example of what you mean? Thanks!
heres an exmaple of a tuturial I found here on flash kit. This is the exact format I would want it in, except the questions that are loaded in are in separate text files. i would like them to come all from one.
Okay, I fixed it to load all the questions from a single XML file (enclosed). I pretty much overhauled all the scripts - it's much simpler now (all except the XML parser on the first frame, which is obtuse).
The questions are loaded once in the first frame and randomly shuffled.
Then the quiz loops thru the questions, by incrementing questionNbr.
The maximum score is no longer hard-coded at 50, but dynamically calculated as number of questions times 10.
Modify the questions.xml file to add more questions to the quiz.
Originally posted by jbum Okay, I fixed it to load all the questions from a single XML file (enclosed). I pretty much overhauled all the scripts - it's much simpler now (all except the XML parser on the first frame, which is obtuse).
The questions are loaded once in the first frame and randomly shuffled.
Then the quiz loops thru the questions, by incrementing questionNbr.
The maximum score is no longer hard-coded at 50, but dynamically calculated as number of questions times 10.
Modify the questions.xml file to add more questions to the quiz.
- jbum
Awesome, that works perfect! Thanks so much, I owe you one!
I'm trying to develop a multiple choice quiz just like described in this thread but I can't open the quiz sample made by jbum. Flash displays an error message "unexpected file format". Can anyone help me with a good sample of a quiz made with flash and xml?
This was very useful. Much obliged. I have adapted it for the non-profit Texas State Historical Association, and am working on making it fully accessible through JAWS. I am still tweaking this, and am interested in knowing the answers to the two questions above my post.
Can you add more questions to the .xml but limit the number of questions asked, or can you create multiple .xml files that are randomly loaded each time the quiz reloads?
If you have an answer statement that corresponds to the individual question, can that text be loaded in the feedback screens?
You can see the file embedded in a php-rendered html page at http://www.tsha.utexas.edu/supsites/quizshow/index.html
I would like to add the .fla here, but it is larger than allowed. I will email it directly if anyone wants to look at it.
Thanks for any assistance or feedback!
Last edited by char74; 02-03-2006 at 06:45 PM.
Reason: could not include fla because of file size, so I had to remove that line.
To answer KMF's question about having 10 questions in the file, replace this code:
maxscore = qList.length*10;
// randomly shuffle list of questions
qList.sort(function() { return random(3) - 1;});
questionNbr = 0;
with this code:
qList.sort(function() { return random(3) - 1;});
questionNbr = qList.length-5;
// determine maximum score
maxscore = (qList.length-questionNbr)*10;
To answer flemming's question:
on the "wrong" frame add a dynamic text field the the variable name of “right” then add another dynamic text box the the field name of choice. Then put some static text next to the boxes that reads “Your choice” next to the “choice” variable field, and “Correct Answer” next the “right” field.
Then you can also add fields that display what the correct and incorrect answer text was if you would like, with this code on the wrong frame:
Then add a field under the choice field with the variable name of “wtext”, and another field under the right field with the variable name of “ctext”.
As for the feedback option. You can do this by adding an extra tag into the XML file like so:
<question q='Who was the First President?'
ans_a='Ulysses S. Grant'
ans_b='Harry Truman'
ans_c='Abraham Lincoln'
ans_d='Albert Einstein'
ans_e='Barny Rubble'
ans_f='George Washington'
>>>>> ans_wrong='Incorrect Feedback would appear here if there was any.'
right='f'>
my choice for the tag was “ans_wrong” as shown above next to the arrows (be sure to remove these in the actual XML file). Then go to frame 2 of your FLA and enter this code under the last letter choice declaration.
feed = qList[questionNbr].ans_wrong;
Then add a another dynamic field on the wrong frame and give it the variable name of “feed”. Any questions, please feel free to ask.
This was very useful. Much obliged. I have adapted it for the non-profit Texas State Historical Association, and am working on making it fully accessible through JAWS. I am still tweaking this, and am interested in knowing the answers to the two questions above my post.
Can you add more questions to the .xml but limit the number of questions asked, or can you create multiple .xml files that are randomly loaded each time the quiz reloads?
If you have an answer statement that corresponds to the individual question, can that text be loaded in the feedback screens?
You can see the file embedded in a php-rendered html page at http://www.tsha.utexas.edu/supsites/quizshow/index.html
I would like to add the .fla here, but it is larger than allowed. I will email it directly if anyone wants to look at it.
Thanks for any assistance or feedback!
Hi I am developing some kind of a quiz for my personal website and would very much need your assistance here. Could you email me the .fla. Thanks in advance. [email protected]
Hi could anyone tell me what is going wrong with my quiz? I want to randomise the order by telling Flash to assign the variable shufflearray to the question nodes but it doesnt seem to work when I try this.
this.checkAnswerNumber=function(userAnswerNumber)
{
if (userAnswerNumber==this.getCorrectAnswerNumber())
gotoAndPlay(22);
else
gotoAndPlay(32);
}
}
function onQuizData(success)
{
var quizNode=this.childNodes[0];
var quizTitleNode=quizNode.firstChild;
title=quizTitleNode.firstChild.nodeValue;
var i=0;
// <items> follows <title>
var itemsNode=quizNode;
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++;
}
//I think you need to make an array 1 to maxNumberOfQuestions i.e
function sortRand () {
if (random(2) == 0) {
return -1;
}
if (random(2) == 0) {
return 1;
}
}
questionarray = [1,2,3,4,5,6,7,8,9,10,11,12];
shuffledArray = questionarray.sort(sortRand); //trace(shuffledArray);
//use this shuffled array to access different nodes of your quiz xml i.e.
nextQuestion = QuizItem[shuffledArray[0]]; <!--This is the line -->
delete shuffledArray[0];
//then delete shuffledArray[0]
////////////
gotoAndStop(12);
}
var quizItems=new Array();
var myData=new XML();
myData.ignoreWhite=true;
myData.onLoad=onQuizData;
myData.load("Quiz 1 - How youll develop the skills & reasons -1144.xml");