I feel like this is fast/easy to solve, but I'm stuck AS 3 problem
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
