Copy link to clipboard
Copied
Hey guys,
So I've been making a few simple maths programs to practice. I made this one as a simple addition/subtraction/multiplication/division tester. It tests addition/subtraction between 1-100 and the twelve times tables for multiplication and division.
I think it's just cos I scripted it a stupid way, but I'm having a few problems and I haven't been able to get it working. Firstly, I'm not entirely sure how to allow a minus sign in the answer box without seemingly upsetting everything. I thought it would just be:
answerBox.restrict = "0-9\\-\\";
or something similar but apparently that messes eveyrthing up when I try. Also, I used a simple 0 or 1 chance mechanic to decide whether or not it would be add/sub or mult/div, then I used the same mechanic to choose "add or sub", or "mult or div". The problem is I only ever get addition or subtraction, it never creates a multiplication or division question (even though I get roughyl 50/50 of add and sub, so the mechanic is working).
Also, it sometimes tells me an answer is wrong when it's right, like a simple addition of 10 + 3 and I give the answer as 13 and it says it's wrong.
I'm not getting ANY error messages or warnings in the compile errors section.
So basically my questions are : is this the best way to make this program? (I'm sure it isn't)
: why won't it display the mult/div ones?
: how do I allow a minus sign while restricting the text input into the answer box?
: why does it sometimes tell me that certain correct answers are wrong?
Thanks, here is the code. Ignore the "emoticon face", that's just a movieclip picture that appears happy or angry whether they are right or wrong
package
{
import flash.display.MovieClip
import flash.events.MouseEvent
public class Main extends MovieClip
{
var numberOne = uint;
var numberTwo = uint;
var answer = int;
var equationSign = String;
var veracity = String;
public function Main()
{
emoticonFace.stop();
answerBox.restrict = "0-9";
questionButton.addEventListener(MouseEvent.CLICK, onQuestionButtonClick);
answerButton.addEventListener(MouseEvent.CLICK, onAnswerButtonClick);
}
function onQuestionButtonClick(event:MouseEvent):void
{
if (Math.round(Math.random()) == 1)
{
if (Math.round(Math.random()) == 1)
{
equationSign = "addition";
}
else
{
equationSign = "subtraction";
}
}
else
{
if (Math.round(Math.random()) == 1)
{
equationSign = "multiplication";
}
else
{
equationSign = "division";
}
}
if (equationSign == "addition" || "subtraction")
{
numberOne = (Math.ceil(Math.random() * 100))
numberTwo = (Math.ceil(Math.random() * 100))
firstNumber.text = numberOne.toString();
secondNumber.text = numberTwo.toString();
if (equationSign == "addition")
{
equationSymbol.text = "+";
}
else if (equationSign == "subtraction")
{
equationSymbol.text = "-";
}
}
else if (equationSign == "multiplication")
{
numberOne = (Math.ceil(Math.random() * 12))
numberTwo = (Math.ceil(Math.random() * 12))
firstNumber.text = numberOne.toString();
secondNumber.text = numberTwo.toString();
equationSymbol.text = "x";
}
else if (equationSign == "division")
{
numberTwo = (Math.ceil(Math.random() * 12))
numberOne = (numberTwo * (Math.ceil(Math.random() * 12)))
firstNumber.text = numberOne.toString();
secondNumber.text = numberTwo.toString();
equationSymbol.text = "/";
}
}
function onAnswerButtonClick(event:MouseEvent):void
{
answer = int(answerBox.text);
if (equationSign == "addition")
{
if (answer == (numberOne + numberTwo))
{
correctIncorrect.text = "That is correct!";
emoticonFace.gotoAndStop(3);
}
else
{
correctIncorrect.text = "That is incorrect!";
emoticonFace.gotoAndStop(2);
}
}
else if (equationSign == "subtraction")
{
if (answer == (numberOne - numberTwo))
{
correctIncorrect.text = "That is correct!";
emoticonFace.gotoAndStop(3);
}
else
{
correctIncorrect.text = "That is incorrect!";
emoticonFace.gotoAndStop(2);
}
}
else if (equationSign == "multiplication")
{
if (answer == (numberOne * numberTwo))
{
correctIncorrect.text = "That is correct!";
emoticonFace.gotoAndStop(3);
}
else
{
correctIncorrect.text = "That is incorrect!";
emoticonFace.gotoAndStop(2);
}
}
else if (equationSign == "division")
{
if (answer == (numberOne / numberTwo))
{
correctIncorrect.text = "That is correct!";
emoticonFace.gotoAndStop(3);
}
else
{
correctIncorrect.text = "That is incorrect!";
emoticonFace.gotoAndStop(2);
}
}
}
}
}
change
var numberOne = uint;
var numberTwo = uint;
var answer = int;
var equationSign = String;
var veracity = String;
to
private var numberOne: uint;
private var numberTwo:uint;
private var answer:int;
private var equationSign:String;
private var veracity:String;
and
if (equationSign == "addition" || "subtraction")
to
if (equationSign == "addition" || equationSign == "subtraction")
and let me know if what probelms you still have
Copy link to clipboard
Copied
change
var numberOne = uint;
var numberTwo = uint;
var answer = int;
var equationSign = String;
var veracity = String;
to
private var numberOne: uint;
private var numberTwo:uint;
private var answer:int;
private var equationSign:String;
private var veracity:String;
and
if (equationSign == "addition" || "subtraction")
to
if (equationSign == "addition" || equationSign == "subtraction")
and let me know if what probelms you still have
Copy link to clipboard
Copied
also change
answerBox.restrict = "0-9";
to
answerBox.restrict = "0-9\\-";
Copy link to clipboard
Copied
Thanks _spoboyle, it works perfectly now! .
I think I understand the other two changes, but why did I have to change the variables to private?
Thanks again
Copy link to clipboard
Copied
ahh sorry you didn't really
I meant you to change
var numberOne = uint;
to
var numberOne:uint;
the reason the private was there was because I mocked up an example in a class rather than on the timeline were i did need them. you don't and can safely delete them again
Copy link to clipboard
Copied
Ohh right haha. Weird it still worked .
Anywho, thanks!!