Hi.
Here is a sample to get you started.
Wish AS3 had some eval function like JavaScript so we could store the expressions in string variables. Although you can use the ExternalInterface class to call JS functions, I'm not sure if it's worth the effort. The workaround I used was to store each expression and the corresponding result in an array.
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
var expressions:Array =
[
{expression:"20/2", result:10},
{expression:"5+8", result:13},
{expression:"12-2", result:10},
{expression:"2*5", result:10},
{expression:"18-6", result:12},
{expression:"23/3", result:7.666666666666667},
{expression:"3*6", result:18},
{expression:"6+4", result:10},
{expression:"8*2", result:16}
];
var correctAnswer:uint = 10; // change the variable type to "int" if you need negative integers or even to "Number" if you need any kind of number
var wrongs:uint = 0; // track the number of correct answers
var corrects:uint = 0; // track the number of wrong answers
var allowedErrors:uint = 3; // how many times the player can miss
var totalCorrects:uint = 0; // it will store the total number of correct answers in the "expressions" array
var time:uint = 30; // total time
var timer:Timer;
function start():void
{
stop();
for (var i:uint = 0, total:uint = buttons.numChildren; i < total; i++)
{
var button:MovieClip = buttons.getChildAt(i) as MovieClip;
button.mouseChildren = false;
button.txt.text = expressions.expression;
if (expressions.result == correctAnswer)
totalCorrects++;
}
startTimer();
buttons.addEventListener(MouseEvent.CLICK, checkAnswer);
}
function checkAnswer(e:MouseEvent):void
{
var mc:MovieClip = e.target as MovieClip;
var childIndex:uint = e.currentTarget.getChildIndex(mc);
if (expressions[childIndex].result == correctAnswer)
{
corrects++;
onCorrect(mc);
}
else
{
wrongs++;
onMiss(mc);
}
if (wrongs == allowedErrors)
onLose();
else if (corrects == totalCorrects)
onWin();
}
function onCorrect(mc:MovieClip):void
{
mc.mouseEnabled = false;
mc.gotoAndPlay("correct");
}
function onMiss(mc:MovieClip):void
{
mc.gotoAndPlay("incorrect");
}
function onWin():void
{
stopTimer();
gotoAndStop("win");
setRestart();
}
function onLose():void
{
stopTimer();
gotoAndStop("lose");
setRestart();
}
function setRestart():void
{
restartButton.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
{
gotoAndStop(1);
});
}
function startTimer():void
{
time = time;
setText();
timer = new Timer(1000, time);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
timer.start();
}
function stopTimer():void
{
timer.removeEventListener(TimerEvent.TIMER, timerHandler);
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
timer.stop();
}
function timerHandler(e:TimerEvent):void
{
time--;
setText();
}
function timerCompleteHandler(e:TimerEvent):void
{
stopTimer();
onLose();
}
function setText():void
{
timerText.text = String(time);
}
start();
I hope this helps.