Can't remove Enter_Frame and stop TimerEvent

Posted by Hwang on Stack Overflow See other posts from Stack Overflow or by Hwang
Published on 2010-04-09T04:35:43Z Indexed on 2010/04/09 4:43 UTC
Read the original article Hit count: 383

I wanted to remove an ENTER_FRAME object and stopping an TimerEvent when I click on a button, and rerun ENTER_FRAME and TimerEvent when I click on another button. I've tried removeAddEventListener and stop() for the time, but I won't work. Any idea whats the problem here?

package{
import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;

public class clockFunction extends MovieClip {

    private var clock:clockMC=new clockMC();

    private var countdownTimer:Timer;

    //seconds
    private var secTop1=clock.second.top1.digit;
    private var secTop2=clock.second.top2.digit;
    private var secBot1=clock.second.bot1.digit;
    private var secBot2=clock.second.bot2.digit;

    private var seconds:Number;
    private var minutes:Number;
    private var hours:Number;
    private var days:Number;

    public function clockFunction():void {
        decrease();
        addChild(clock);
    }


    private function decrease():void {
        countdownTimer=new Timer(1000);

        //Adding an event listener to the timer object
        countdownTimer.addEventListener(TimerEvent.TIMER,updateTime);

        //Initializing timer object
        //countdownTimer.start();
    }

    private function updateTime(event:TimerEvent):void {
        decreasTimerFunction();
        clock.second.play();

        if (seconds==1) {
            clock.minute.play();

        }

        if ((minutes==1)&&(seconds==1)) {
            clock.hour.play();
        }

        if ((hours==1)&&(minutes==1)&&(seconds==1)) {
            clock.day.play();
        }
    }

    //Setting it back to its correct time so it won't have number changing in between of flipping issues.
    private function detect(event:Event):void {
        //seconds
        var sec1=seconds;
        var sec2=seconds-1;

        if (sec1<10) {
            sec1="0"+sec1;
        }

        if (sec2<10) {
            sec2="0"+sec2;
        }

        if (sec1==00) {
            sec2=59;
        }

        secTop1.text=sec1;
        secTop2.text=sec2;
        secBot1.text=sec1;
        secBot2.text=sec2;
    }


    public function startTime():void {
        addEventListener(Event.ENTER_FRAME,detect);
        countdownTimer.start();
        trace("start");
    }

    public function stopTime():void {
        countdownTimer.stop();
        removeEventListener(Event.ENTER_FRAME,detect);
        trace("stop");
    }

    private function decreasTimerFunction():void {

        //Create a date object for Christmas Morning
        var endTime:Date=new Date(2010,3,26,20,0,0);

        //Current date object
        var now:Date=new Date();

        // Set the difference between the two date and times in milliseconds
        var timeDiff:Number=endTime.getTime()-now.getTime();


        seconds=Math.floor(timeDiff/1000);
        minutes=Math.floor(seconds/60);
        hours=Math.floor(minutes/60);
        days=Math.floor(hours/24);

        // Set the remainder of the division vars above
        hours%=24;
        minutes%=60;
        seconds%=60;
    }
}

}

© Stack Overflow or respective owner

Related posts about actionscript-3

Related posts about enterframeevent