A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 35

Thread: external questions file

  1. #1
    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!

  2. #2
    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

  3. #3
    Member
    Join Date
    Oct 2003
    Posts
    63
    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!

  4. #4
    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.

  5. #5
    Member
    Join Date
    Oct 2003
    Posts
    63
    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!

  6. #6
    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.

  7. #7
    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

  8. #8
    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 Attached Files

  9. #9
    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 Attached Files

  10. #10
    Member
    Join Date
    Oct 2003
    Posts
    63
    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!

  11. #11
    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?

  12. #12
    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?

  13. #13
    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.

  14. #14
    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

  15. #15
    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.

  16. #16
    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

  17. #17
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    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.

  18. #18
    Junior Member
    Join Date
    Mar 2006
    Posts
    1
    Can you please email to me too? Please. Thank You

    email: smaz_titanium@hotmail.com

  19. #19
    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

  20. #20
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center