Copy link to clipboard
Copied
Hi
I have created a quiz using as3. This is the code I have used for radioboxes:
var quizscore = 0; score.text = quizscore+"";
var group1:RadioButtonGroup = new RadioButtonGroup("question1");
a1.group = a2.group = a3.group = a4.group = group1;
submit_btn.addEventListener(MouseEvent.CLICK, myquiz)
function myquiz (event:MouseEvent):void{
if(group1.selection.label=="Correct Answer"){quizscore+=10; nextFrame(); }
else{ nextFrame(); }
}
I have a multiple choice question so want to use checkboxes. can I use the same sort of method for checkboxes?
ie. if the selected checkbox value is 'this' then add 10 points. This question has two correct answers; have can I limit how many checkboxes are checked before they hit submit?
can anyone help?
Many thanks : )
1 Correct answer
Since you're using frames as the mechanism to change the quiz, the scripts on each frame can mutate a bit to handle each question easily.
In each frames script you can decide which values are correct for that frame and use the existing logic you're using to determine if something is correct. Using the checkbox selected property will tell you which are selected so compare that to the correct answer, e.g.:
...
if ( (checkbox1.selected && checkbox2.selected && checkbox999.selected)
{
// correct, do w
Copy link to clipboard
Copied
Since you're using frames as the mechanism to change the quiz, the scripts on each frame can mutate a bit to handle each question easily.
In each frames script you can decide which values are correct for that frame and use the existing logic you're using to determine if something is correct. Using the checkbox selected property will tell you which are selected so compare that to the correct answer, e.g.:
if ( (checkbox1.selected && checkbox2.selected && checkbox999.selected)
{
// correct, do what you want
} else {
// incorrect, do something else
}
There's the other side as well, you'd typically want to verify some checkboxes are not checked (wrong answers), so just add ! in front, e.g: if (checkbox1.selected && !checkbox2.selected) { // 1 is selected, 2 is not }.. If you have some questions with 20 possible answers the strategy isn't elegant so mention if you do.
Limiting the number of checkboxes that can be checked is possible in a variety of ways. The easiest is telling the user how many can be checked, allowing them to check as many as they want and upon submitting the answer verify the number of answers is within your limits. It's not the best way but it's easiest. Just literally loop through checkboxes and count. If you name the answer checkboxes with a number (checkbox1, checkbox2, ...checkbox99) you can even iterate through them that way, e.g.:
// set a range from 1-3 answers
function checkAnswers(e:MouseEvent):void
{
// answer range
var minAnswers:int = 1;
var maxAnswers:int = 3;
// number detected as selected
var selectedAnswers = 0;
// total number of checkboxes for this question (checkbox1, checbox2, ... checkbox20)
var totalCheckboxes:int = 5;
for (var checkboxNumber:int = 1; checkboxNumber <= totalCheckboxes; checkboxNumber++)
{
if (this['checkbox' + checkboxNumber].selected == true)
{
// increment selected answers
selectedAnswers++;
}
}
// simple check for if it's in range
if ( selectedAnswers >= minAnswers && selectedAnswers <= maxAnswers)
{
// number is within range, continue
}
else
{
// out of range, warn user about the expected number of answers
}
}
When they click whatever button you have you can have the handler do what it does and also fire off that function..
myButton.addEventListener(MouseEvent.CLICK, checkAnswers);
There are much cleaner ways to do it but since the question is basic I'm just giving you a simple way to handle the solution. If you're much more into scripting and reusability I can suggest other ways but just as they're less tedious, they're more complicated.
Copy link to clipboard
Copied
wow thank you so much for this!!
I will be checking seprartely if the correct answer has been selected and then add the score if they have selected the right answer (if that makes sense?!)
This is my code so far to make sure the score adds if they have selected the right answers:
score.text = myscore+"";
b4.addEventListener(MouseEvent.CLICK, quizHandler4);
function quizHandler4(event:MouseEvent):void{
if (ans1.selected){ myscore+=10; nextFrame()}
if (ans2.selected){ myscore+=10; nextFrame()}
else{nextFrame() }
}
I get this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at quiz_fla::MainTimeline/__setProp_an4_Scene1_content_12()
at quiz_fla::MainTimeline/frame13()
at flash.display::MovieClip/nextFrame()
at quiz_fla::MainTimeline/quizHandler4()
The quiz is very simple there will be a maximum of 2 or 3 correct answers.
What am I missing? will I also have to add the the code for incorrect answers as well? as this handler runs when the submit button is clicked?
Thank you so much for taking the time to right that code and helping me! I really appreciate it.
Thanks again.
Copy link to clipboard
Copied
don`t know abou the specifics of your quiz but its likely when the quizhandler runs
that your nextFrame() function jumps to a frame where ans2 is not available
so rewrite your function to:
function quizHandler4(event:MouseEvent):void{
if (ans1.selected){ myscore+=10;}
if (ans2.selected){ myscore+=10;}
nextFrame();
//you don`t need an else condition since it defaults to the same action as your ifs
}
Copy link to clipboard
Copied
Thank you so much.
You both got my quiz working!
Many thanks
Copy link to clipboard
Copied
As mocca mentioned you're adding 10 to the score and running nextFrame() if the user has selected either checkbox. I don't believe you intend to do that so mocca's code is showing you that you should remove it.
It's a bit easier to just show you an example. The code will look similar but different from above. The main reason is I'm re-using functions and variables defined on the first frame in the second frame. So the code going forward each frame is very simple.
Click on the CheckBox components and the submit Button to see I've kept to a strict naming convention so this works. All you'd need to do to add a new question is add a blank frame, paste the code from frame 2 (adjust to taste) and change the questions just like frame #2.
Saved down to CS5 example:
http://www.ertp.com/tmp/AS3Quiz.zip
edit: Haha, posted during your post.. Anyhow it's an alternate way to consider if you wish.
Glad you got it working, you're welcome and good luck!

