如何在as3中使用timerEvent更改布尔值

时间:2014-04-08 12:31:08

标签: actionscript-3 flash actionscript-2 flash-cs5 flash-cs4

我写了这个小代码来改变0到1的布尔值,反之亦然 但它不起作用。

结果是它始终保持为0.我必须犯一些愚蠢的错误。请帮忙。感谢

  var booleanL:Number = 0;
            var myTimerL:Timer = new Timer(1000,60);
            myTimerL.addEventListener(TimerEvent.TIMER, timerListenerL);
                function timerListenerL (e:TimerEvent):void{
                    if(booleanL == 0) {
                        booleanL = 1;
                    } else if(booleanL == 1) {
                        booleanL = 0;
                    }
                }
            myTimerL.start();
            trace(booleanL);

1 个答案:

答案 0 :(得分:0)

编辑: 您可以尝试使用计算机时钟来制作秒表。每当时钟的end_time time [延迟]秒高于start_time时,布尔值就会更新。延迟由此行设置:此处delay = start_time.seconds + 2;在更新前提供+2秒延迟作为限制

尝试这样......

    import flash.display.MovieClip;
    import flash.utils.Timer;
    import flash.utils.getTimer;
    import flash.events.TimerEvent;
    import flash.events.Event; //added this for enter frame events

    public class timer extends MovieClip 
    {
        public var booleanL:int = 0;

        public var start_time:Date = new Date;
        public var end_time:Date =  new Date;
        public var delay:int;

        public function timer() 
        {
               //var myTimerL:Timer = new Timer(1000, 5);
               //myTimerL.addEventListener(TimerEvent.TIMER, timerListenerL);
               //myTimerL.start();

               timer_Reset();
        }

        function timerListenerL ():void //(event:TimerEvent):void
        {
              trace("Am updating Boolean...");
              if( 1 == booleanL) 
              { booleanL = 0; trace(booleanL); } 
              else if (0 == booleanL) 
              { booleanL = 1; trace(booleanL);}
        }

        //USE REAL TIME CLOCK VERSION

        public function timer_Reset():void 
        {
                start_time = new Date; //reset time to now..
                delay = start_time.seconds + 2; //two seconds test delay
                stage.addEventListener(Event.ENTER_FRAME, _update);
        }

        function _update (e:Event):void
        {
                //set end_time to now-time every frame, 
                //then check if end seconds are [+ delay] higher than start_time.seconds

                end_time = new Date(); //set to Now time

                if (end_time.seconds ==  delay)
                {
                  trace(end_time.hours + ":" + end_time.minutes + ":" + end_time.seconds);
                  stage.removeEventListener(Event.ENTER_FRAME, _update);
                  timerListenerL (); //we update boolean  via this function
                  timer_Reset(); //we reset for next check via this function
                }
        }

}