To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 03-16-2004, 01:27 PM   #1
kmf82
Member
 
Join Date: Oct 2003
Posts: 63
external questions file

Hello,

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!
kmf82 is offline   Reply With Quote
Old 03-16-2004, 02:02 PM   #2
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
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
__________________
jbum is offline   Reply With Quote
Old 03-16-2004, 02:15 PM   #3
kmf82
Member
 
Join Date: Oct 2003
Posts: 63
Quote:
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!
kmf82 is offline   Reply With Quote
Old 03-16-2004, 02:18 PM   #4
Shotsy247
Senior Member
 
Shotsy247's Avatar
 
Join Date: Apr 2001
Location: Be there in a minute!
Posts: 1,386
Another way to work with one external txt file.


Your external Text File titled questions.txt

questionList=Question1?, Question 2?, Here is another question?, And yet another?, This one is actually a statement!


in you movie:

questionSet = new LoadVars(this)
questionSet.load("questions.txt");
questionSet.onLoad = function(success){
if(success){
questionsArray = new Array();
questionsArray = questionSet.questionList.split(",");
}
}

Now your questions can be accessed through the array.

questionsArray[1];
questionsArray[2];
questionsArray[3];

and so on.

or have i = your random number and...

questionsArray[i];

I'm not up on the XML method although I think I will use this as a reference and try to learn it.

_t
__________________
I don't feel tardy.
Shotsy247 is offline   Reply With Quote
Old 03-16-2004, 02:29 PM   #5
kmf82
Member
 
Join Date: Oct 2003
Posts: 63
Quote:
Originally posted by Shotsy247
Another way to work with one external txt file.


Your external Text File titled questions.txt

questionList=Question1?, Question 2?, Here is another question?, And yet another?, This one is actually a statement!


in you movie:

questionSet = new LoadVars(this)
questionSet.load("questions.txt");
questionSet.onLoad = function(success){
if(success){
questionsArray = new Array();
questionsArray = questionSet.questionList.split(",");
}
}

Now your questions can be accessed through the array.

questionsArray[1];
questionsArray[2];
questionsArray[3];

and so on.

or have i = your random number and...

questionsArray[i];

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!
kmf82 is offline   Reply With Quote
Old 03-16-2004, 02:59 PM   #6
Shotsy247
Senior Member
 
Shotsy247's Avatar
 
Join Date: Apr 2001
Location: Be there in a minute!
Posts: 1,386
What kind of questions? Do mean like multiple choice or something?

_t
__________________
I don't feel tardy.
Shotsy247 is offline   Reply With Quote
Old 03-16-2004, 03:28 PM   #7
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
Why don't you make a simple mockup of how you want your quiz to look, with a sample question or two, and post the .fla here.

Then it will be easier for us to work these examples into your framework...



- jim
__________________
jbum is offline   Reply With Quote
Old 03-16-2004, 03:51 PM   #8
kmf82
Member
 
Join Date: Oct 2003
Posts: 63
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.
Attached Files
File Type: zip quiz.zip (8.7 KB, 1144 views)
kmf82 is offline   Reply With Quote
Old 03-16-2004, 04:35 PM   #9
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
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
Attached Files
File Type: zip quiz.zip (5.5 KB, 2062 views)
__________________
jbum is offline   Reply With Quote
Old 03-16-2004, 05:13 PM   #10
kmf82
Member
 
Join Date: Oct 2003
Posts: 63
Quote:
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!
kmf82 is offline   Reply With Quote
Old 03-16-2004, 05:15 PM   #11
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
Yes you do. Got any small children or extra limbs?
__________________
jbum is offline   Reply With Quote
Old 03-16-2004, 05:23 PM   #12
kmf82
Member
 
Join Date: Oct 2003
Posts: 63
one more question. Say I have 10 questions in the file, what part of the code would I have to modify to only pull out say 5 of those questions?
kmf82 is offline   Reply With Quote
Old 08-26-2005, 11:31 AM   #13
ramzez
Senior Member
 
Join Date: Apr 2003
Location: Never Never Land
Posts: 120
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?

I'm using Flash MX.
ramzez is offline   Reply With Quote
Old 12-05-2005, 07:25 AM   #14
flemming
Member
 
Join Date: Sep 2000
Posts: 70
Showing the correct answer !

Hi There ... this helped me a lot getting a quiz on our
sailingclub website...

Is there a way to showing the RIGHT answér when you hit a wrong:

Like
Wrong

the rights answer is: Because its fun! (C ans_c='Because its fun!')




Have a nice day
flemming is offline   Reply With Quote
Old 02-03-2006, 06:43 PM   #15
char74
Junior Member
 
Join Date: Feb 2006
Posts: 1
Questions on Questions

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.
char74 is offline   Reply With Quote
Old 02-08-2006, 08:04 AM   #16
samuelodofin
Junior Member
 
Join Date: Feb 2006
Posts: 1
Hi

Hey Please can you send it to samuelodofin@yahoo.com ??..................am dieing to have that stuff.
Thanks
samuelodofin is offline   Reply With Quote
Old 03-10-2006, 10:53 AM   #17
jweeks123
Senior Member
 
jweeks123's Avatar
 
Join Date: Mar 2006
Posts: 1,069
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:

if(choice=="a") {
wtext=answera;
}
if(choice=="b") {
wtext=answerb;
}
if(choice=="c") {
wtext=answerc;
}
if(choice=="d") {
wtext=answerd;
}
if(choice=="e") {
wtext=answere;
}
if(choice=="f") {
wtext=answerf;
}
if(right=="a") {
ctext=answera;
}
if(right=="b") {
ctext=answerb;
}
if(right=="c") {
ctext=answerc;
}
if(right=="d") {
ctext=answerd;
}
if(right=="e") {
ctext=answere;
}
if(right=="f") {
ctext=answerf;
}
stop();

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.
jweeks123 is offline   Reply With Quote
Old 03-30-2006, 02:58 AM   #18
ATitaniumA
Junior Member
 
Join Date: Mar 2006
Posts: 1
Can you please email to me too? Please. Thank You

email: smaz_titanium@hotmail.com
ATitaniumA is offline   Reply With Quote
Old 05-25-2006, 12:28 AM   #19
mook-it
Junior Member
 
Join Date: May 2006
Posts: 1
Quote:
Originally Posted by char74
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.
m_okumu@hotmail.com
mook-it is offline   Reply With Quote
Old 05-31-2006, 07:16 AM   #20
tasteofthevine
Junior Member
 
Join Date: Apr 2006
Posts: 5
My Quiz

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.

The code is below:

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(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");

Thanks

Chris Girgg
tasteofthevine is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:39 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.