views:

10

answers:

1

I'm trying to create a simple flex4 project which involves some timers that trigger other functions.

I don't have much experience with Action Script and even less with timer events.

Here is a bit of my code it seems to be working for the most part but you lines were I'm adding up the total score (score = score +1;) seems to just keep adding and adding when I test the application. I think its because the timers keep firing the function but I'm not sure.

            private var score:int = 0;

        private function submit():void {
            this.currentState = 'loading';
            var timer:Timer = new Timer(2200);
            timer.addEventListener(TimerEvent.TIMER, removeLoading);
            timer.start();
        }

        private function removeLoading(event:TimerEvent):void{
            removeloading.play();
            var timer1:Timer = new Timer(1000);
            timer1.addEventListener(TimerEvent.TIMER, viewResults);
            timer1.start();
            this.currentState = 'results';
        }

        private function viewResults(event:TimerEvent):void{

            if (q1_t.selected == true){
                answer1m.text = 'You Answer the Question Correctly.';
                score = score +1;

            } else {
                answer1m.text ='The Correct answer was: '+ q1_t.label;
            }

            if (q2_f.selected == true){
                answer2m.text = 'You Answer the Question Correctly.';
                score = score +1;

            } else {
                answer2m.text ='The Correct answer was: '+ q2_f.label;
            }

            finalscore.text = score.toString();
        }
+1  A: 

So I did a bit more research and turns out I hadn't included the second timer parameter.

The second parameter is the number of times that the TimerEvent.TIMER event will be dispatched before stopping. If you set the second parameter to 0 (zero) or omitted it completely, the timer would run forever (or until you called the stop() method on the timer instance.

Since I only want to run the event once I need to add 1.

From this:

var timer:Timer = new Timer(2200);

To this:

var timer:Timer = new Timer(2200,1);
Adam