Flash / Flex / ActionScript/Animation/Timer — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 08:14, 26 мая 2010

Change text value in a TextField in a timer

 
package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    
    public class Main extends Sprite {
        var timer:Timer = new Timer(1000);
        
        private var _text:TextField;
        private var _start:uint;
        
        public function Main(  ) {
            _start = 0;
            _text = new TextField(  );
            addChild(_text);
            timer.addEventListener(TimerEvent.TIMER, onTimer);
            timer.start(  );
        }
        
        private function onTimer(event:TimerEvent):void {
            _text.text = (timer.currentCount - _start) + " milliseconds";
        }
        
    }
}



Creating a Timer

 
package Main{
    import flash.display.Sprite;
    import flash.utils.*;
    import flash.events.*;
    public class Main extends Sprite {
        public static const TIMER_DELAY:Number = 1 * 1000;
        public var timer:Timer;
        public function Main() {
            timer = new Timer(TIMER_DELAY, 10);
        }
    }
}



Creating Symbols Within Flash CS3

 
package {
    import flash.display.Sprite;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    public class Main extends Sprite {
       var timer:Timer;
       public function Main () {
           super();
           timer = new Timer(1000);
           timer.addEventListener(TimerEvent.TIMER, bark);
           timer.start();
       }
       public function bark(event:TimerEvent = null):void {
           trace("Woof!");
       }
   }
}



Defers the call to the deferredMethod( ) method for five seconds:

 

package{
  import flash.display.Sprite;
  import flash.utils.*;
  import flash.events.*;
  
  public class Main extends Sprite{
    public function Main(){
        var timer:Timer = new Timer(5000, 1);
        timer.addEventListener(TimerEvent.TIMER, deferredMethod);
        timer.start(  );
    }
    function deferredMethod(event:TimerEvent):void {
          trace("on timer");
    }
  }
}



draws a single line with a random stroke style every 250 milliseconds. It uses clear( ) to erase the previously drawn line.

 
package {
  import flash.display.*;
  import flash.utils.*;
  import flash.events.*;
  public class Main extends Sprite {
    private var s:Shape;
    public function Main(  ) {
      s = new Shape(  );
      addChild(s);
      var t:Timer = new Timer(250);
      t.addEventListener(TimerEvent.TIMER, timerListener);
      t.start(  );
    }
    private function timerListener (e:TimerEvent):void {
      s.graphics.clear(  );
      s.graphics.lineStyle(random(1, 10), random(0, 0xFFFFFF));
      s.graphics.moveTo(random(0, 550), random(0, 400));
      s.graphics.lineTo(random(0, 550), random(0, 400));
    }
    public function random (minVal:int, maxVal:int):int {
      return minVal + Math.floor(Math.random(  ) * (maxVal + 1 - minVal));
    }
  }
}



Post-Event Updates for Timer Events

 
package {
  import flash.display.*;
  import flash.events.*;
  import flash.utils.*;
  public class Main extends Sprite {
    public function Main(  ) {
      stage.frameRate = 1;
      var timer:Timer = new Timer(100, 0);
      timer.start(  );
      timer.addEventListener(TimerEvent.TIMER, timerListener);
    }
    private function timerListener (e:TimerEvent):void {
      var rect:Sprite = new Sprite(  );
      rect.graphics.lineStyle(1);
      rect.graphics.beginFill(0x0000FF);
      rect.graphics.drawRect(0, 0, 150, 75);
      rect.x = Math.floor(Math.random(  )*stage.stageWidth);
      rect.y = Math.floor(Math.random(  )*stage.stageHeight);
      addChild(rect);
      e.updateAfterEvent(  )
    }
  }
}



Repeating a Task over Time

 
package {
    import flash.display.Sprite;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    
    public class Main extends Sprite {
        private var _PreviousTime:Number = 0;
        
        public function Main(  ) {
            var tTimer:Timer = new Timer(500, 10);
            tTimer.addEventListener(TimerEvent.TIMER, onTimer);
            tTimer.start(  );
        }
        
        private function onTimer(event:TimerEvent):void {
            trace(flash.utils.getTimer(  ) - _PreviousTime);
            _PreviousTime = flash.utils.getTimer(  ); 
        }
    }
}



Two timers are set, one for a square sprite and one for a circle sprite

 
package {
    import flash.display.Sprite;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    
    public class Main extends Sprite {
        private var _square:Sprite;
        private var _circle:Sprite;
        
        
        public function Main(  ) {
            _square = new Sprite(  );
            _square.graphics.beginFill(0xff0000);
            _square.graphics.drawRect(0, 0, 100, 100);
            _square.graphics.endFill(  );
            addChild(_square);
            _square.x = 100;
            _square.y = 50;
            
            _circle = new Sprite(  );
            _circle.graphics.beginFill(0x0000ff);
            _circle.graphics.drawCircle(50, 50, 50);
            _circle.graphics.endFill(  );
            addChild(_circle);
            _circle.x = 100;
            _circle.y = 200;
            
            var squareTimer:Timer = new Timer(50, 0);
            squareTimer.addEventListener(TimerEvent.TIMER, onSquareTimer);
            squareTimer.start(  );
            
            var circleTimer:Timer = new Timer(100, 0);
            circleTimer.addEventListener(TimerEvent.TIMER, onCircleTimer);
            circleTimer.start(  );
        }
        
        private function onSquareTimer(event:TimerEvent):void {
            _square.x++;
        }
        
        private function onCircleTimer(event:TimerEvent):void {
            _circle.x++;
        }
    }
}