A Flash Developer Resource Site

Page 2 of 2 FirstFirst 12
Results 21 to 35 of 35

Thread: external questions file

  1. #21
    Junior Member
    Join Date
    Aug 2006
    Posts
    18
    Hi I am developing some kind of a quiz for my personal tutorial in flash and am in need of some help here. Could you email me the .fla. Thanx in advance.
    genie_rs2000@hotmail.com

    thanx again

  2. #22
    DesignMercenary
    Join Date
    Aug 2006
    Posts
    10

    HTML in question text?

    Hopefully someone is still reading this...

    I stumbled across this thread a few weeks back and it has proven invaluable in creating a reusable quiz framework for a client who is setting up interactive kiosks inside a large facility. My sincere thanks to everyone who contributed. However...

    I have added an "explanation" attribute to the XML question tags, so that after an answer is chosen, they are shown "right" or "wrong" and then an explanation comes up which shows the correct answer and some extra information. Like so:

    <question q="Which planet is closest to the Sun?"
    ans_a="Pluto"
    ans_b="Mercury"
    ans_c="Uranus"
    ans_d="Saturn"
    ans_exp="Mercury is the innermost planet in our solar system, orbiting the Sun once every 88 days."
    right="b">

    This works beautifully, but the problem now is that the client wishes to add some HTML/CSS support to the explanation, to highlight the correct answer within the text (ans_exp="<b>Mercury</b> is the innermost planet ..." with the <b> being styled with inline or an external CSS doc).

    But of course adding HTML tags to the <question> tag attributes breaks the XML load.

    I'm relatively new to using XML in Flash, but I'm thinking that the most likely change that needs to be made is to set up the XML using nested elements instead of attributes, like so:

    <question>
    <q>Which planet is closest to the Sun?</q>
    <ans_a>Pluto</ans_a>
    <ans_b>Mercury</ans_b>
    <ans_c>Uranus</ans_c>
    <ans_d>Saturn</ans_d>
    <ans_exp><b>Mercury</b> is the innermost planet in our solar system, orbiting the Sun once every 88 days.</and_exp>
    <right>b</right>
    </question>

    However, I have no idea how to accomplish this transition.

    Is this the correct approach? Is there an easier way?

  3. #23
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    You'd need to wrap your HTML text inside a CDATA tag in order to avoid breaking the XML...ie:
    code:

    <ans_exp><![CDATA[<b>Mercury</b> is the innermost planet in our solar system, orbiting the Sun once every 88 days.]]></and_exp>


    Consequently you'll need to adjust your XML parsing to drill down another node into the CDATA...

    HTH,

    K.

  4. #24
    DesignMercenary
    Join Date
    Aug 2006
    Posts
    10

    not quite

    deadbeat - Thanks for the response. However, I'm still working from an XML file that uses single <question> tags w/attributes, like so:

    <question q="Which planet is closest to the Sun?"
    ans_a="Pluto"
    ans_b="Mercury"
    ans_c="Uranus"
    ans_d="Saturn"
    ans_exp="Mercury is the innermost planet in our solar system, orbiting the Sun once every 88 days."
    right="b">

    And I can't put the CDATA tag within the attribute...

  5. #25
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    Well, then you'll either need to restructure the XML, or abandon the idea of adding HTML tags to it...

    K.

  6. #26
    DesignMercenary
    Join Date
    Aug 2006
    Posts
    10
    Right. And what I'm asking is, if I restructure the XML, how do I get the AS to parse it?

    Maybe this phrases the question better:

    The following AS:

    [frame 1]
    Code:
    qList = new Array();
    nbrUsed = 0;
    loadNode = function(it) {
     if (it.nodeName == 'question') {
     var qSet = new Array();
     qSet.q = it.attributes['q'];
     qSet.used = false;
     qSet.ans_a = it.attributes['ans_a'];
     qSet.ans_b = it.attributes['ans_b'];
     qSet.ans_c = it.attributes['ans_c'];
     qSet.ans_d = it.attributes['ans_d'];
     qSet.ans_exp = it.attributes['ans_exp'];
     qSet.right = it.attributes['right'];
     qList.push(qSet);
     }
     if (it.hasChildNodes()) {
     for (var i = 0; i < it.childNodes.length; ++i)
      loadNode(it.childNodes[i]);
     }
    }
    
    function finishInit() {
     trace(qList.length + " questions loaded");
     qList.sort(function() {return random(3) - 1;});
     questionNbr = qList.length-5;
     maxscore = (qList.length-questionNbr); 
     gotoAndPlay(2);
    }
    
    var quizXML = new XML();
    quizXML.onLoad = function() {
     qList = new Array(); 
     loadNode(this);
     finishInit();
    }
    
    quizXML.load("quiz.xml");
    [frame 2]
    Code:
    if (questionNbr >= qList.length)
     gotoAndPlay("end");
    
    question = qList[questionNbr].q;
    answerA  = qList[questionNbr].ans_a;
    answerB  = qList[questionNbr].ans_b;
    answerC  = qList[questionNbr].ans_c;
    answerD  = qList[questionNbr].ans_d;
    explain  = qList[questionNbr].ans_exp;
    right    = qList[questionNbr].right;
    Parses an XML document with multiple tags like this:

    Code:
    <question q="Which planet is closest to the Sun?"
     ans_a="Pluto"
     ans_b="Mercury" 
     ans_c="Uranus"
     ans_d="Saturn"
     ans_exp="Mercury is the innermost planet in our solar system, orbiting the Sun once every 88 days."
     right="b">
    This works perfectly. What I WANT to be able to do is include HTML tags inside the text. So, assuming I restructure the XML like so (with CDATA tag as per deadbeat's response):

    Code:
    <question>
     <q>Which planet is closest to the Sun?</q>
     <ans_a>Pluto</ans_a>
     <ans_b>Mercury</ans_b> 
     <ans_c>Uranus</ans_c>
     <ans_d>Saturn</ans_d> 
    <ans_exp><![CDATA[<b>Mercury</b> is the innermost planet in our solar system, orbiting the Sun once every 88 days.]]></ans_exp>
     <right>b</right>
    </question>
    What has to happen to the AS to properly create the question array from this new XML?

  7. #27
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    Something like this should work:
    code:

    if (it.nodeName == 'question') {
    var nodes=it.childNodes;
    qSet.q = nodes[0].firstChild.nodeValue;
    qSet.used = false;
    qSet.ans_a = nodes[1].firstChild.nodeValue;
    qSet.ans_b = nodes[2].firstChild.nodeValue;
    qSet.ans_c = nodes[3].firstChild.nodeValue;
    qSet.ans_d = nodes[4].firstChild.nodeValue;
    qSet.ans_exp = nodes[5].firstChild.nodeValue;
    qSet.right = nodes[6].firstChild.nodeValue;
    qList.push(qSet);
    }


    K.

  8. #28
    DesignMercenary
    Join Date
    Aug 2006
    Posts
    10

    almost

    Okay, this code appears to be loading the restructured XML file (the trace(qList.length) is showing the correct # of questions loaded), but none of the question or answer text fields are displaying text.

    Does the frame 2 code (below) need to change at all?

    Code:
    // Select next question in array (which is already randomly shuffled)
    if (questionNbr >= qList.length)
    	gotoAndPlay("end");
    
    question = qList[questionNbr].q;
    answerA  = qList[questionNbr].ans_a;
    answerB  = qList[questionNbr].ans_b;
    answerC  = qList[questionNbr].ans_c;
    answerD  = qList[questionNbr].ans_d;
    explain  = qList[questionNbr].ans_exp;
    right    = qList[questionNbr].right;

  9. #29
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    Try adding a couple of traces for debugging purposes:
    code:

    if (questionNbr >= qList.length)
    gotoAndPlay("end");
    }
    trace("length= "+qList.length);
    trace("questionNbr= "+questionNbr);
    var item=qList[questionNbr];
    trace("item= "+item);

    for(var i in item){
    trace(i+" = "+item[i]);
    }


    K.

  10. #30
    Junior Member
    Join Date
    Jul 2000
    Posts
    7

    Multiple 'rounds'.

    Getting back to the original post about creating a Quiz....

    Everything is working perfectly for me as is, but I would like to add in one more function. My quiz has 3 difficultly levels. I've modified the quiz that Jim originally posted to jump to a frame called 'round2' when all the questions have been answered correctly. This part works fine.

    Once in 'round2' I duplicated the movieclip that ran the initial set of questions and just modified the code to look for 'questions2.xml' (as opposed to 'questions.xml' in the 'round1' movieclip. The problem I get is that the movie automatically jumps to the end (as the code tells it to do in frame 2 if all the questions have been answered). I tried removing this line of code to see where the problem started and now whereever the questions/answers should be displayed it says 'undefined / undefined / undefined /undefined / undefined'.

    Is this happening because it's using the same code as before to look in an Array for what's been used and what hasn't? I tried adding a 2 to all the functions and variables (i.e. q is now q2 / question is now question2) and still nothing.

    Little help...

    - Chris
    user:nex
    www.projectecho.net

  11. #31
    Junior Member
    Join Date
    Jul 2000
    Posts
    7
    Nevermind. There was a problem with my 'questions2.xml' file. I had an apostrophe in one of the questions and that was screwing everything up. It works fine now.
    user:nex
    www.projectecho.net

  12. #32
    Junior Member
    Join Date
    Oct 2005
    Posts
    9

    Next button for Quiz

    Hey All,

    how would I change the code above to pause before going to the next question?

    I know I would need to make the button invisible until the user clicked the answer but I am a newbie at actionsript and am not quite sure how to proceed

  13. #33
    Junior Member
    Join Date
    Oct 2005
    Posts
    9
    I PM'ed you char74 hope that doesn't bother you...I wanted to know if I could get a opy of what you did on the Texas site...I was able to get jBums script to work, however I need the ability to provide feedback..I have a clcunky way of doing it...but it doesn't keep score correctly after I hit the next button...I would love to see your end result .FLA hopefully now my haor will start growing back.

    Thanks I will PM you my email address
    Last edited by geebee2; 01-03-2007 at 07:56 PM.

  14. #34
    Junior Member
    Join Date
    Oct 2005
    Posts
    9
    Quote Originally Posted by jweeks123
    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.
    How do you put a pause button to allow the user to see the feedback....I tried your code and it shows the feeback at lightning speed

  15. #35
    Junior Member
    Join Date
    May 2009
    Posts
    1
    Hi,
    Now I would like to random the answers. How can I do that? Thanks for your help.

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