problem trying to make a quiz/questionnaire

can't get the 'next' button to behave itself

Page 1 of 1

3 Replies - 11316 Views - Last Post: 21 September 2006 - 07:08 AM

#1 smallfatguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-June 05

problem trying to make a quiz/questionnaire

Posted 18 September 2006 - 09:20 PM

Attached File  quiz.xml (729bytes)
Number of downloads: 1162Hello all again,

I am trying to make my own quiz or questionnaire using Flash MX 2004. Although I can get the data to load from an external XML file ok, and I can set it up so that the next button will bring me through the whole questionnaire, I am now trying to set it up so that the next button won't work until you click an option on each question. . I have created two main movie clips - one with the introduction screen which also contains the start button, and one with the actual dynamic text boxes for the questions and radio buttons for the answers - and also the misbehaving 'next' button. If you test run the movie attached below, you will see that it works for the first question, but not afterwards. And I don't see why not. The 'checkAnswer' function should be called each time you click the next button, which in turn should wait until a choice is made before allowing the nextQuestion function to be called.

I know its a fairly poorly organised bit of code, but I am learning as I go, and trying to add in what I am learning. I am hoping someone will have the patience to wade through this mire and find (and maybe even explain to me) what it is I am doing wrong.

Many thanks in advance

SFG

Attached File  quiz.xml (729bytes)
Number of downloads: 1162


Sorry,

for some reason it is not allowing me to upload the fla. So here is the code I have used so far. Hopefully, with the description of how I have organised the objects on the stage this will make enough sense for someone to work out what is happening.

stop();

function checkAnswer(){
	var choice:Number = question_mc.radioGroup.selection.data;
	if (choice){
		nextQuestion();
	}
	else {
		question_mc.next_btn.setEnabled(false);
	}
}


function nextQuestion(){
	question_mc.option1.setState(false);
	question_mc.option2.setState(false);
	question_mc.option3.setState(false);
	question_mc.option4.setState(false);
	if(currentQuestion < qList.length){
		currentQuestion += 1;
		getInfo(currentQuestion-1);
		}
	}

function getInfo(num){
	infoArray = new Array();
	infoArray = qList[num].childNodes;
	for (i=0;  i<=infoArray.length; i++){
		element = infoArray[i].nodeName;
		if (element == "question"){
			question_mc.question_txt.text = infoArray[i].firstChild.nodeValue;
		}
		if (element == "option1"){
			question_mc.option1.label = infoArray[i].firstChild.nodeValue;
		}
		if (element == "option2"){
			question_mc.option2.label = infoArray[i].firstChild.nodeValue;
		}
		if (element == "option3"){
			question_mc.option3.label = infoArray[i].firstChild.nodeValue;
		}
		if (element == "option4"){
			question_mc.option4.label = infoArray[i].firstChild.nodeValue;
		}
		if (element == "answer"){
			correctAnswer = infoArray[i].firstChild.nodeValue;
		}
	}
}

function showIt(){
	currentQuestion = 1;
	getInfo(currentQuestion - 1);
}

function loadQuiz(){
	var quiz:XML = new XML();
	quiz.load("quiz.xml");
	quiz.ignoreWhite = true;
	quiz.onload = function(success){
		if (success){
			qList = new Array();
			qList = this.firstChild.childNodes;
			showIt();
		}
		else {
			status_txt.text = "the data failed to load";
		}
	}
}

function startQuiz(){
	startScreen_mc._visible = false;
	question_mc._visible = true;
	loadQuiz();
}

function initialise(){
	question_mc._visible = false;
	finalScreen_mc._visible = false;
}

initialise();
startScreen_mc.start_btn.addEventListener("click", this.startQuiz);
question_mc.next_btn.addEventListener("click", this.checkAnswer);


thanks again for looking

Is This A Good Question/Topic? 0
  • +

Replies To: problem trying to make a quiz/questionnaire

#2 sandro  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 01-September 06

Re: problem trying to make a quiz/questionnaire

Posted 19 September 2006 - 12:31 PM

View Postsmallfatguy, on 18 Sep, 2006 - 09:20 PM, said:

Attachment attachmentHello all again,

I am trying to make my own quiz or questionnaire using Flash MX 2004. Although I can get the data to load from an external XML file ok, and I can set it up so that the next button will bring me through the whole questionnaire, I am now trying to set it up so that the next button won't work until you click an option on each question. . I have created two main movie clips - one with the introduction screen which also contains the start button, and one with the actual dynamic text boxes for the questions and radio buttons for the answers - and also the misbehaving 'next' button. If you test run the movie attached below, you will see that it works for the first question, but not afterwards. And I don't see why not. The 'checkAnswer' function should be called each time you click the next button, which in turn should wait until a choice is made before allowing the nextQuestion function to be called.

I know its a fairly poorly organised bit of code, but I am learning as I go, and trying to add in what I am learning. I am hoping someone will have the patience to wade through this mire and find (and maybe even explain to me) what it is I am doing wrong.

Many thanks in advance

SFG

Attachment attachment


Sorry,

for some reason it is not allowing me to upload the fla. So here is the code I have used so far. Hopefully, with the description of how I have organised the objects on the stage this will make enough sense for someone to work out what is happening.

stop();

function checkAnswer(){
	var choice:Number = question_mc.radioGroup.selection.data;
	if (choice){
		nextQuestion();
	}
	else {
		question_mc.next_btn.setEnabled(false);
	}
}


function nextQuestion(){
	question_mc.option1.setState(false);
	question_mc.option2.setState(false);
	question_mc.option3.setState(false);
	question_mc.option4.setState(false);
	if(currentQuestion < qList.length){
		currentQuestion += 1;
		getInfo(currentQuestion-1);
		}
	}

function getInfo(num){
	infoArray = new Array();
	infoArray = qList[num].childNodes;
	for (i=0;  i<=infoArray.length; i++){
		element = infoArray[i].nodeName;
		if (element == "question"){
			question_mc.question_txt.text = infoArray[i].firstChild.nodeValue;
		}
		if (element == "option1"){
			question_mc.option1.label = infoArray[i].firstChild.nodeValue;
		}
		if (element == "option2"){
			question_mc.option2.label = infoArray[i].firstChild.nodeValue;
		}
		if (element == "option3"){
			question_mc.option3.label = infoArray[i].firstChild.nodeValue;
		}
		if (element == "option4"){
			question_mc.option4.label = infoArray[i].firstChild.nodeValue;
		}
		if (element == "answer"){
			correctAnswer = infoArray[i].firstChild.nodeValue;
		}
	}
}

function showIt(){
	currentQuestion = 1;
	getInfo(currentQuestion - 1);
}

function loadQuiz(){
	var quiz:XML = new XML();
	quiz.load("quiz.xml");
	quiz.ignoreWhite = true;
	quiz.onload = function(success){
		if (success){
			qList = new Array();
			qList = this.firstChild.childNodes;
			showIt();
		}
		else {
			status_txt.text = "the data failed to load";
		}
	}
}

function startQuiz(){
	startScreen_mc._visible = false;
	question_mc._visible = true;
	loadQuiz();
}

function initialise(){
	question_mc._visible = false;
	finalScreen_mc._visible = false;
}

initialise();
startScreen_mc.start_btn.addEventListener("click", this.startQuiz);
question_mc.next_btn.addEventListener("click", this.checkAnswer);


thanks again for looking


Hi smallfatguy,

if you need to upload a file you have to zip it first.
In any case I believe the problem you are having is due to a wrong check in the checkAnswer function.
You have set
var choice:Number
but then you check it as if it was a boolean
Since the variable choice exists this will always return true.
Try something like
if(choice != undefined)
or something else to test its numeric value

Hope this helps
Sandro
Was This Post Helpful? 0
  • +
  • -

#3 smallfatguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-June 05

Re: problem trying to make a quiz/questionnaire

Posted 19 September 2006 - 03:33 PM

Sandro,

many thanks for your reply. I see what you mean by the inappropriate check I was making. I have changed the code to what you suggested, but it doesn't help. Probably because once a value has been given to the variable in the first question, it is no longer undefined. I have managed to attached the file as a zip, like you suggested.

I wll just have to keep experimenting and hoping for some insight to strike me from somewhere. Thanks again for looking and for spotting the error.

Attached File  quiz00.zip (184.15K)
Number of downloads: 582

SFG
Was This Post Helpful? 0
  • +
  • -

#4 timgen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-September 06

Re: problem trying to make a quiz/questionnaire

Posted 21 September 2006 - 07:08 AM

Try replacing your nextQuestion function with this:

function nextQuestion(){
	question_mc.option1.setState(false);
	question_mc.option2.setState(false);
	question_mc.option3.setState(false);
	question_mc.option4.setState(false);
	if(currentQuestion == qList.length){
		finalScreen_mc._visible = true;
		question_mc._visible = false;
	} else {
		currentQuestion += 1;
		getInfo(currentQuestion-1);
	}
}


Basically, I made it check your current question to see if it is the last BEFORE it tries to load in the next node (which would have been NULL).

Let me know if that works! :pirate:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1