[RESOLVED] That Quiz Again
A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: [RESOLVED] That Quiz Again

  1. #1
    Senior Member
    Join Date
    Mar 2008
    Posts
    168

    resolved [RESOLVED] That Quiz Again

    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.

    Any help would be greatly appreciated.
    Last edited by ADVaughn; 07-28-2012 at 11:07 AM.

  2. #2
    Senior Member Steven FN's Avatar
    Join Date
    Mar 2010
    Location
    CA, USA
    Posts
    276
    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.

    Change your for loop to the following:

    for (var i=1; i<currentQuizItem.getNumOfAnswers(); i++)
    {
    _root["answer"+i]=currentQuizItem.getAnswer(i-1);
    _root["answer"+i+"Mark"]._visible=true;
    _root["answer"+i+"Textbox"]._visible=true;
    _root["btn0"+i]._visible=true;
    }

    Hope that makes sense.

  3. #3
    Senior Member Steven FN's Avatar
    Join Date
    Mar 2010
    Location
    CA, USA
    Posts
    276
    Also, instead of parsing the "rightresponse" from the xml, you could just use:

    rightresponse="Right Response for Question "+currentQuestionNumber;

    Because the response is the same but with the added question number.

  4. #4
    Senior Member
    Join Date
    Mar 2008
    Posts
    168
    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.

    Thanks for your help
    Last edited by ADVaughn; 06-23-2012 at 05:12 PM.

  5. #5
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,808
    Hi,

    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>
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  6. #6
    Senior Member
    Join Date
    Mar 2008
    Posts
    168
    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.

  7. #7
    Senior Member Steven FN's Avatar
    Join Date
    Mar 2010
    Location
    CA, USA
    Posts
    276
    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>
    Last edited by Steven FN; 06-24-2012 at 12:30 AM.

  8. #8
    Senior Member
    Join Date
    Mar 2008
    Posts
    168
    Steven thanks, I actually had done just that but when I compile it no longer shows the last answer choice in each question.
    Last edited by ADVaughn; 07-28-2012 at 11:07 AM.

  9. #9
    Senior Member Steven FN's Avatar
    Join Date
    Mar 2010
    Location
    CA, USA
    Posts
    276
    Try replacing these actions on frame 12, line 64 and down is the new stuff:

    Code:
    var currentQuizItem=quizItems[currentQuestionNumber-1];
    var hasAnswered=false;
    question=currentQuizItem.getQuestion();
    //trace(currentQuizItem.answers);
    
    for (var i=1; i<=4; i++)
    {	
    	_root["answer"+i]=currentQuizItem.getAnswer(i-1);
    	if(i>currentQuizItem.getNumOfAnswers()){
    		_root["answer"+i+"Mark"]._visible=false;
    		_root["answer"+i+"Textbox"]._visible=false;
    		_root["btn0"+i]._visible=false;
    	}else{
    		_root["answer"+i+"Mark"]._visible=true;
    		_root["answer"+i+"Textbox"]._visible=true;
    		_root["btn0"+i]._visible=true;
    	}
    }
    
    stop();
    Basically runs thru all the buttons and makes the buttons visible/invisible depending on the number of answers.

  10. #10
    Senior Member
    Join Date
    Mar 2008
    Posts
    168
    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.

  11. #11
    Senior Member Steven FN's Avatar
    Join Date
    Mar 2010
    Location
    CA, USA
    Posts
    276
    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>
    Last edited by Steven FN; 06-24-2012 at 04:53 PM.

  12. #12
    Senior Member
    Join Date
    Mar 2008
    Posts
    168
    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();

  13. #13
    Senior Member
    Join Date
    Mar 2008
    Posts
    168
    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>

    </item>

  14. #14
    Senior Member Steven FN's Avatar
    Join Date
    Mar 2010
    Location
    CA, USA
    Posts
    276
    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"){

    Code:
    if(answerNode.nodeName == "answer"){
    	quizItems[i].addAnswer(answerNode.firstChild.nodeValue, isCorrectAnswer);
    }else{
    	if(answerNode.nodeName == "rightresponse"){
    		quizItems[i].rightresponse = answerNode.firstChild.nodeValue;
    	}else{
    		quizItems[i].wrongresponse = answerNode.firstChild.nodeValue;
    	}
    }
    Hope that makes sense.

  15. #15
    Senior Member
    Join Date
    Mar 2008
    Posts
    168
    Works brilliantly thanks so much for your help, I can finally put this to rest.

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

Poll by Flashkit.com