Hello, I posted yesterday an article similar to this but I went back over the tutorial on OOP http://www.kirupa.com/net/oop_intro_classes_pg1.htm and made some changes but it seems that there is still something wrong. I’m just trying to display a simple question through a class file, but its not working.
Here is what I had in .fla file that I converted to .as. The .fla file worked so the issue is setting up my class file.
var myEquations:Array = [];
var myOperators:Array = ["+","-","*","/"];
for (var i:Number = 0;i < 10;i++) {
var num1:Number = Math.round((Math.random() * 10) + 1);
var num2:Number = Math.round((Math.random() * 10) + 1);
var randOp:Number = Math.round(Math.random() * (myOperators.length - 1));
var temp:String = num1.toString() + " " + myOperators[randOp] + " " + num2.toString();
trace("this is random string " + i + ": " + temp);
myEquations.push(temp);
}
//first, I'll set up a variable to hold
//the current equation we're on.
var currentEq:Number = 0;
//now here's a function that will place
//the equation in the left textfield.
var setEquation:Function = function() {
eq_txt.text = myEquations[currentEq];
}
//And, now that our equations are all inside the
//myEquations array, let's see how
//we can compute them:
var checkFunction:Function = function() {
var ta:Array = myEquations[currentEq].split(" ");
trace("here's ta: " + ta);
var tmp:Number;
switch(ta[1]) {
case "+" :
trace("we're adding");
tmp = parseInt(ta[0]) + parseInt(ta[2]);
break;
case "-" :
trace("we're subtracting");
tmp = parseInt(ta[0]) - parseInt(ta[2]);
break;
case "*" :
trace("we're multiplying");
tmp = parseInt(ta[0]) * parseInt(ta[2]);
break;
case "/" :
trace("we're dividing");
tmp = parseInt(ta[0]) / parseInt(ta[2]);
break;
}
trace("and tmp is: " + tmp);
if (tmp.toString() == ans_txt.text) {
trace("correct!");
ans_txt.text = "";
currentEq ++;
setEquation();
} else {
trace("wrong!");
}
}
setEquation();
check_mc.onRelease = checkFunction;
//Here is where I have converted the code into a class file
class Mathlesson {
//declare class variables this is the variable set up to get the users info from the text box.
var ans_txt: String;
/*******************************************************************************/
//class constructor
function Mathlesson() {
var ans_txt= userinput;
}
/*******************************************************************************/
//define methods sets up the question to be displayed
public function getuserinput():String {
return userinput;
}
public function checkFunction() {
var ta:Array = myEquations[currentEq].split("");
var tmp:Number;
switch(ta[1]) {
case "+" :
trace ("we're adding");
tmp = parseInt(ta[0]) + parseInt(ta[2]);
break;
case "-" :
trace ("we're subtrating");
tmp = parseInt(ta[0]) + parseInt(ta[2]);
break;
case "*" :
trace("we're multiplying");
tmp = parseInt(ta[0]) + parseInt(ta[2]);
break;
case "/" :
trace ("we're dividing");
tmp = parseInt(ta[0]) + parseInt(ta[2]);
break;
}
trace ("and tmp is: " +tmp);
if (tmp.toString() == ans_txt.text) {
trace("Correct!");
ans_txt.text="";
currentEq ++;
setEquation();
} else {
trace("wrong!");
}
}
setEquation();
check_mc.onRelease =checkFunction;
/********************************************************************************
//sets up the question to be displayed
*********************************************************************************/
private function displayquest(){
var myEquations:Array = [];
var myOperators:Array = ["+","-","*","/"];
for (var i:Number = 0;i < 10;i++) {
var num1:Number = Math.round((Math.random() * 50) + 1);
var num2:Number = Math.round((Math.random() * 50) + 1);
var randOp:Number = Math.round(Math.random() * (myOperators.length - 1));
var temp:String = num1.toString() + " " + myOperators[randOp] + " " + num2.toString();
//var temp:String = num1.toString() + myOperators[randOp] + num2.toString();
myEquations.push(temp);
}
var currentEq:Number =0;
var setEquation:Function = function() {
eq_txt.text = myEquations[currentEq];
}
}
}
I would appreciate any help. I’m not too sure what I’m doing wrong.