Go Back   kirupaForum > Flash > Game/AI Programming

Reply
 
Thread Tools Display Modes
Old 12-05-2009, 05:16 AM   #1
babeinwoods
Registered User
Barcode XML Flash Quiz - manipulating xml data

Hello,

As my handle suggests, Flash, XML and Actionscripting are pretty much a foreign language for me, so any help I can get with this issue would be immensely appreciated.

I found a tutorial online on creating a flash quiz application, using xml data for the questions. The quiz works fine - I even managed to add a countdown timer and randomize the questions. BUT... I need to be able to have 50 questions in the xml file, and only import 15 random questions to the flash file. How do I do that?

Can anyone help?

Lost in the Woods
babeinwoods is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?

Old 12-05-2009, 09:28 AM   #2
glosrfc
Registered User
 
glosrfc's Avatar
Location Halley Research Station, Latitude 75°35' S, Longitude 26°39' W, Brunt Ice Shelf, Coats Land, Antarctica

Posts 4,765
The simplest way would be to import all 50 questions from your XML file, place them into an array, randomise the array, then read the first 15 items.

This example uses an external text file, but the principle is still the same:
http://www.kirupa.com/forum/showthread.php?t=337408

Other examples and clarification:
http://www.kirupa.com/forum/showthread.php?p=2414453
http://www.kirupa.com/forum/showthread.php?t=307291
http://www.kirupa.com/forum/showthread.php?t=331116

__________________
©2006 GlosRFC - Searching 8,168,684,336 brain cells
glosrfc is offline   Reply With Quote
Old 12-05-2009, 05:08 PM   #3
babeinwoods
Registered User
Hey GlosRFC,

I'm still going through the links you posted, so I can't boast success as yet, but thank you for the quick reply. I appreciate it.

babeinwoods
babeinwoods is offline   Reply With Quote
Old 12-06-2009, 12:24 PM   #4
babeinwoods
Registered User
No luck!

I'm trying various ways to do this, and have spent two days going through various tutorials, books and the Flash Help section, but if I get one thing right, the other goes wrong - for instance, if I specify how many questions to be shown, the movie doesn't show the questions at all! If I shuffle them, it loads all the questions... With one attempt I just kept getting 'undefined parameter'. Can I share the code and get help? Here it is:

Code:
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++;
    }

     function randList(max:Number, num:Number, shuffled:Boolean):Array {  
     var tempArray:Array = [];  
     for (i = 0; i < max && num > 0; ++i) {  
      if (Math.floor(Math.random() * (max - i)) < num) {  
       tempArray.push(i + 1);  
       num--;  
      }  
     }  
     if (shuffled) {  
     tempArray.sort(function () {  
      return Math.floor(Math.random() * 2) ? true : false;  
     });  
    }  
    return (tempArray);  
   }  
    
    gotoAndStop("Start");
}

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

stop();
This code has a shuffle function, and I am supposed to add "var myArr:Array = randList(50, 15, true)" somewhere on the timeline, but when I do, the questions don't load. Without it, everything works smoothly, except the shuffle itself.

Also, on the timeline, this is the code:

Code:
if (currentQuestionNumber>quizItems.length)
   gotoAndStop("SummaryScreen");
 
var currentQuizItem=quizItems[currentQuestionNumber-1];
var hasAnswered=false;
question=currentQuizItem.getQuestion();
for (var i=1; i<=4; i++)
{
    _root["answer"+i]=currentQuizItem.getAnswer(i-1);
}

stop();
I'd really appreciate any help in getting this to work. Again, I'd like to have 50 questions, out of which each session of game play would only display 15 random questions.

Last edited by babeinwoods; 12-06-2009 at 12:28 PM..
babeinwoods is offline   Reply With Quote
Old 12-06-2009, 02:05 PM   #5
babeinwoods
Registered User
I GOT IT!

Who'd have thought? All it took was wading through flash.livedocs and learning all about arrays.

Still a babe in the woods.
babeinwoods is offline   Reply With Quote
Reply

Tags
flash, quiz, xml


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 10:21 AM.

SHARE:

SUPPORTERS:

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com