I completely re-wrote this quiz and had it running perfectly then my buddy asked if he could have T&F questions.
No problem I created a variable "numOfAnswers" then a fucntion to check the value for the number of answers "numOfAnswers". I then set a for loop and when the number of answers is less that 4 (hence T&F) make the instances for answer text 3 & 4 and their buttons invisable this is on T&F questions.
Problem One:
My hide function sort of works, it hides answer text and button for #4 but not #3 on a True and False question. This I can't understand why 4 but not 3.
Problem Two:
When you answer the question correctly it returns a "Correct Response" along with telling the user they got it correct. When you run it you will see that works on the questions with 4 answers but not on the T & F questions with just two answers.
The numOfAnswers is 5, but there are only 4 answers. Since you start "i=1", the loop iterates through 5 times (1,2,3,4,5) and due to the "<=" when "i=5" the loop still runs through. So, just change the "<=" to "<", makes the loop stop at 4 iterations.
Thanks Steve your change fixed problem 1 now it is hiding 3&4 on T&F.
But I still have problem 2. Actually the response is different for each question. As you can see when you answer 1 correctly you get "Right Response for Question 1". The response in the re now is temporary for testing eventually the responses will be drastically different. On T&F the field comes up undefined on T&F.
problem two is occurring because you set the XML object to load the 5th node as the response, but in T&F questions, there are only 4 nodes, as opposed to normal 4-question nodes, which have 6 nodes. There is no 6th node in T&F questions in the XML file, which is why it's loading undefined. You can either change the script to detect when T&F questions are appearing, and make the XML object look for the 4th node as the response, or you can simply create 2 dummy nodes:
Code:
<item>
<question>True or false: Disney's Pluto was a Rabbit.</question>
<answer>True</answer>
<answer correct="y">False</answer>
<answer />
<answer />
<rightresponse>Right Response for Question 2.</rightresponse>
</item>
17 Years old boy, who loves the Computer Technology
BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS
Hey buddy wish it were that easy. If I do that I loose the function that hides answers 3&4 on T&F. I was thinking about soing something in the for loop on Fame 12 where I am checking for the number of answer nodes, but nothing I tried works.
You need to parse out the "answer" nodes separately. Your code adds the "rightresponse" to the answer array, so you need to sort the nodes by node name and add the values to the proper variables like so:
// <answer> follows <question>
var answerNode=itemNode.childNodes[a++];
while (answerNode)
{var isCorrectAnswer=false;
if (answerNode.attributes.correct=="y"){
isCorrectAnswer=true;
}
if(answerNode.nodeName == "answer"){
quizItems[i].addAnswer(answerNode.firstChild.nodeValue, isCorrectAnswer);
}else{
quizItems[i].rightresponse = answerNode.firstChild.nodeValue;
}
// goto the next <answer>
got it that makes perfect sence. I am new to XML and it has been a real lesson. Started with a simple quiz, all multiple choice, and have been adding features slowly.
I think I am done with this one, if I did one more thing it would be to add a "response" for incorrect answers as well but I had such a hard time adding the one for "correct responses"...
Thanks for your help I had been reading about parseing nodes by name but wasn't having a lot of luck.
Glad i could help. XML takes a while to get a hold on, especially with AS2. AS3 has better XML handling for sure.
Also, if you wanted to add an "incorrect response", it would fairly simple with our new code. Just check the answerNode by the nodeName like we did for separating the answers for the answerArray.
Code:
if(answerNode.nodeName == "wrongresponse"){
quizItems[i].wrongresponse = answerNode.firstChild.nodeValue;
}
// XML <wrongresponse>Wrong Response for Question #</wrongresponse>
Plugged in code for Wrong Response after updating the XML file as well as the keyframes that call the data. Almost working with NEW wrong answer response but for some reason it is pulling the wrong response in from the XML for the correct response, in other words both right and wrong response dynamic field is being populated with same XML node "wrong response"
Code:
//XML Quiz with correct response by Mark Pinkston pinkstonms@yahoo.com
function QuizItem(question,right_response)
{
this.question=question;
this.rightresponse=right_response;
this.wrongresponse=wrong_response;
this.answers=new Array();
this.numOfAnswers=0;
this.correctAnswer=0;
this.getQuestion=function()
{
return this.question;
}
this.getrightresponse=function(){
return this.rightresponse;
}
this.getwrongresponse=function(){
return this.wrongresponse;
}
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");
}
/*CHECK TO SEE IF LESS THAN 4 ANSWERS (T/F)*/
this.getNumOfAnswers=function(){
return this.answers.length;
}
}
function onQuizData(success)
{var quizNode=this.firstChild;
var quizTitleNode=quizNode.firstChild;
title=quizTitleNode.firstChild.nodeValue;
numberOfQuestionsToDisplay=int(quizNode.childNodes[1].firstChild.nodeValue);
trace("numberOfQuestionsToDisplay="+numberOfQuestionsToDisplay);
var i=0;
var itemsNode=quizNode.childNodes[2];
while (itemsNode.childNodes[i])
{
var itemNode=itemsNode.childNodes[i];
var questionNode=itemNode.childNodes[0];
quizItems[i]=new QuizItem(questionNode.firstChild.nodeValue, itemNode.childNodes[5].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;
}
// Check <answer> and read node by name>
if(answerNode.nodeName == "answer"){
quizItems[i].addAnswer(answerNode.firstChild.nodeValue, isCorrectAnswer);
}else{
quizItems[i].rightresponse = answerNode.firstChild.nodeValue;
quizItems[i].wrongresponse = answerNode.firstChild.nodeValue;
}
// goto the next <answer>
answerNode=itemNode.childNodes[a++];}i++;
quizItems[i]=new rightresponse(rightresponseNode.firstChild.nodeValue, rightresponse);
}
for (i=0; i<quizItems.length; i++)
gotoAndPlay(_currentFrame);
}
var quizItems=new Array();
var myData=new XML();
myData.ignoreWhite=true;
myData.onLoad=onQuizData;
myData.load("payday_W.xml");
stop();
here is an item in the XML:
<items>
<item>
<question>1+1=?</question>
<answer>1</answer>
<answer correct="y">2</answer>
<answer>3</answer>
<answer>4</answer>
<rightresponse>wrong response 1</rightresponse>
<wrongresponse>wrong response 1</wrongresponse>
You need to add another IF statement to filter between the rightresponse and wrongresponse. For your code, the first IF checks to see if it's an ANSWER node, then if it's not it sets the same value to rightresponse and wrongresponse both. So, you have to use another IF to separate the 2 variables and set different values for each, like so:
IF answer node THEN addAnswer ELSE IF rightresponse node THEN set rightresponse ELSE set wrongresponse
The second IF is saying if it's not an answer node or a rightresponse node, then set the value to wrongresponse. You could make another IF statement if you wanted to: if(answerNode.nodeName == "wrongresponse"){