Copy link to clipboard
Copied
I thought I'd teach my grade 10 programming class Action Scripting 3.0. We've already gone through SmallBasic and they understand conditional logic, looping etc.. to give you a bit of background so I thought we'd tackle it in an OOP language.
I haven't tackled A.S. since back in 2.0 and I think maybe I'm missing something. I used this tutorial as a foundation and updated it to reflect AS3.0 for my course notes.
That said, when making their quiz, (a simple 2 question quiz that tallies the score at the end of the quiz) I keep getting a logic error. Everything runs smoothly using the controls, I'm getting the variables return properly to the Interpreter pane, but the logic evaluation at the end returns a glitch where it counts against my last clicked value and uses it twice to evaluate truth. Likely I'm missing something simple and it'll take somebody who knows Flash well about 3 seconds to ID my problem.
Thanks in advance.
FLA file if you want to browse it:
SWF file if you want to view that:
If you don't want to dload the files then here's AS code at the 3 locations (q1, q2, quizEnd)
Code Block 1 at frame 1:
// Initialize main timeline variables
// next, create variables to store user's answers:
var q1Answer; // User's answer for question 1
var q2Answer; // User's answer for question 2
//next, create a variable to track number of questions answered correctly:
var totalCorrect = 0; // Counts number of correct answers
//finally, stop the movie at the first question:
stop();
choice1_btn.addEventListener(MouseEvent.CLICK, answer1);
function answer1(event_object:MouseEvent){
q1Answer = 1;
gotoAndStop("q2");
trace(q1Answer);
}
choice2_btn.addEventListener(MouseEvent.CLICK, answer2);
function answer2(event_object:MouseEvent){
q1Answer = 2;
gotoAndStop("q2");
trace(q1Answer);
}
choice3_btn.addEventListener(MouseEvent.CLICK, answer3);
function answer3(event_object:MouseEvent){
q1Answer = 3;
gotoAndStop("q2");
trace(q1Answer);
}
Code Block 2 at frame 10
choice1_btn.addEventListener(MouseEvent.CLICK, answer21);
function answer21(event_object:MouseEvent){
q2Answer = 1;
gotoAndStop("quizEnd");
trace(q2Answer);
}
choice2_btn.addEventListener(MouseEvent.CLICK,answer22);
function answer22(event_object:MouseEvent){
q2Answer = 2;
gotoAndStop("quizEnd");
trace(q2Answer);
}
choice3_btn.addEventListener(MouseEvent.CLICK,answer23);
function answer23(event_object:MouseEvent){
q2Answer = 3;
gotoAndStop("quizEnd");
trace(q2Answer);
}
Code Block 3 at frame 20
if (q1Answer == 3) {
totalCorrect = totalCorrect + 1;
trace("Value for Q1 is 1 point");
} else {
trace("Wrong answer #1 " + q1Answer);
}
if (q2Answer == 3) {
totalCorrect++; //simply adds 1 more to the previous value of totalCorrect//
trace("Value for Q2 is 1 point");
} else {
trace("Wrong answer #2 " + q2Answer);
}
//displays final score on interpreter
trace("Your total is "+totalCorrect+"/2 points");
//displays the answer on the stage
var txtFld:TextField = new TextField();
addChild(txtFld);
txtFld.text = ("Your total is" + totalCorrect);
txtFld.appendText ("/2 points");
//extra bits to un-comment once text output is understood
//txtFld.wordWrap = true;
//txtFld.textColor = 0xFF00FF
//txtFld.width=150
//txtFld.height = 60
oh i see the problem.
you're using the same button names for q1 and q2 and you're not removing any listeners. you need to fix that.
Copy link to clipboard
Copied
looks ok.
except for the "/2 points", which looks flakey, which trace is giving an unexpected result?
Copy link to clipboard
Copied
Let's say I click on button1 at frame 1 returning a value for q1Answer as 1, then button 3 at frame 10 returning a value for q2Answer as 3, I'll get both questions marked as right. Button 3 is the "right button" in each case so only q1Answer = 3 and q2Answer=3 should be correct.
Here's the trace output if I click as above:
1
3
Value for Q1 is 1 point
Value for Q2 is 1 point
Your total is 2/2 points
3
If I click on 1 and 2 say instead, here's the Interpreter result:
1
2
Wrong answer #1 2
Wrong answer #2 2
Your total is 0/2 points
2
Mystifying.
kglad wrote:
looks ok.
which trace is giving an unexpected result?
Copy link to clipboard
Copied
oh i see the problem.
you're using the same button names for q1 and q2 and you're not removing any listeners. you need to fix that.
Copy link to clipboard
Copied
You sir, are a god. Thank you!
Frustrating, I guess the button names were global, I thought they'd be relegated to being relevant only to that frame, but clearly not.
Thanks again.
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now