Log in

View Full Version : Dynamic Flash Quiz using XML



Mofoquer
03-04-2009, 10:36 PM
Hello all

Im new around here and I was wondering if someone could help me with some codes, as I got to the point that I'm kinda stuck :p

I made a flash animation, and in one of the scenes there's a Trivia game (quiz), that loads as Movie Symbol, with all the scripting inside. All the questions are called from a XML file and everything works great.

The thing is, that I can make the questions random everytime I play, but now I need to make also the answers to appear in different possitions within the questions; call me dumb or anything, but I have no knowledge in how to do that :P

So, any help would be greatly apreciated.

Thanks.

Medyman
03-05-2009, 12:50 AM
Take all of the answer choices and add them to an array. Next, sue the array methods to shuffle them.

More info: http://mrsteel.wordpress.com/2007/05/26/random-array-in-as2-as3-example-using-sort/

Mofoquer
03-05-2009, 05:31 AM
hello,

where exactly do I put this code?

Mofoquer
03-05-2009, 06:12 AM
To be more specific, here's the code:

On frame 1 i have this 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_e = it.attributes['ans_e']; // just in case there are more than 4...
qSet.ans_f = it.attributes['ans_f'];
qSet.right = it.attributes['right'];
qList.push(qSet);
}
else if (it.nodeName == "questionList")
{
}
if (it.hasChildNodes()) {
for (var i = 0; i < it.childNodes.length; ++i)
loadNode(it.childNodes[i]);
}
}

function finishInit()
{
trace(qList.length + " questions loaded:");
maxscore = qList.length*10;
qList.sort(function() { return random(5) - 1;});
questionNbr = 0;
gotoAndPlay(2);
}

var myxml = new XML();

myxml.onLoad = function()
{
qList = new Array();

loadNode(this);
finishInit();
}

myxml.load("questions.xml");
stop();


On frame 2:

{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;
right = qList[questionNbr].right;
}

{if (questionNbr >= qList.length)
gotoAndPlay("end2");
question = qList[questionNbr].q;
answera = qList[questionNbr].ans_a;
answerb = qList[questionNbr].ans_b;
answerc = qList[questionNbr].ans_c;
answerd = qList[questionNbr].ans_d;
wrong = qList[questionNbr].wrong;
}

-on frame 3:
stop();

-on frame 4 (here, the buttons and the dynamic text box connected with the script are shown here):

if (right eq choice) {
gotoAndPlay("right");
} else {
gotoAndPlay("wrong");
}

The next frames are frames with labels "right""wrong" and "end"
each one witha script in a frame with the middle"

-right:
score = 10;
questionNbr++;
gotoAndPlay(2);

-wrong:
score =5;
questionNbr++;
gotoAndPlay(2);

and the button answers are labeled: A, B, C, D for the button answers (where you click) ans the answer next to them (Dynamic text boxes), are answera, answerb, answerc, answerd

The "questions.xml" that loads in the first frame, it's where all the questions are. Something like this:

<question q='What's the purpouse of this website?'
ans_a='meet friends'
ans_b='waste time'
ans_c='finding answers and meet cool people'
ans_d='play games'
right='c''>

When it finishes, it ends in a frame labaled "end" with a message.

The thing is, that I want to randomize the position of the answers also, and, if you didn't click the correct answers (lets say 3 of 7), that jumps to another frame besides the "end" with the message "congratulations you passsed", to another frame, lets say "end2" that says "i'm sorry, you failed"

I hope I was more especific this time, and if anyone needed the above script for anything, I'm glad I could help, as you can't find it so easily on the web :P

Peace

Mofoquer
03-05-2009, 11:19 PM
somebody out there that knows about this?

Sunnyme
03-13-2009, 09:43 AM
please have a peek at http://www.trap17.com/index.php/php-quiz-script_t8729.html

for flash quiz only if you're flash developer, it goes easier i think, try this:
http://sn.im/dq1mo