Flash / Flex / ActionScript/Animation/Frame Event

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

Add Enter Frame event listener

 
package {
    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.text.TextField;
    import flash.events.Event;
    public class Main extends Sprite {
        private var _bitmap:BitmapData= new BitmapData(100, 100, false, 0xffffffff);
        private var _textfield:TextField= new TextField(  );
        public function Main(  ) {
            var image:Bitmap = new Bitmap(_bitmap);
            addChild(image);
            _bitmap.noise(1000, 0, 255, 1|2|4, false);
    
            addChild(_textfield);
            _textfield.y = 100;
    
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
    
        public function onEnterFrame(event:Event):void {
            var colorVal:Number = _bitmap.getPixel(mouseX, mouseY)
            _textfield.text = "#" + colorVal.toString(16).toUpperCase(  );
        }
    }
}



Animating with the ENTER_FRAME Event: Animating a TextField horizontally

 
package{
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;
    public class Main extends Sprite {
      private var t:TextField;
    
      public function Main(  ) {
        t = new TextField(  );
        t.text          = "Hello";
        t.autoSize      = TextFieldAutoSize.LEFT;
        addChild(t);
    
        addEventListener(Event.ENTER_FRAME, moveTextRight);
      }
    
      public function moveTextRight (e:Event):void {
        t.x += 10;
      }
    }
}



Designated Frame Rate Versus Actual Frame Rate

 
package {
  import flash.display.*;
  import flash.text.*;
  public class Main extends Sprite {
    public function Main(  ) {
      var t:TextField = new TextField(  );
      trace(stage.frameRate.toString());
    }
  }
}



Measuring the actual frame rate

 
package {
  import flash.display.*;
  import flash.events.*;
  import flash.utils.*;
  import flash.text.*;
  public class Main extends Sprite {
    private var lastFrameTime:Number;
    private var output:TextField = new TextField(  );
    public function Main() {
      output.autoSize = TextFieldAutoSize.LEFT;
      output.border     = true;
      output.background = true;
      output.selectable = false;
      addChild(output);
      addEventListener(Event.ENTER_FRAME, enterFrameListener);
    }
    private function enterFrameListener (e:Event):void {
      var now:Number = getTimer(  );
      var elapsed:Number = now - lastFrameTime;
      var framesPerSecond:Number = Math.round(1000/elapsed);
      output.text = "Time since last frame: " + elapsed
             + "\nExtrapolated actual frame rate: " + framesPerSecond
             + "\nDesignated frame rate: " + stage.frameRate;
      lastFrameTime = now;
    }
  }
}



Move TextField in animation

 
package{
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;
    public class Main extends Sprite {
      private var t:TextField;
    
      public function Main() {
        t = new TextField();
        t.text          = "Hello";
        t.autoSize      = TextFieldAutoSize.LEFT;
        addChild(t);
    
        addEventListener(Event.ENTER_FRAME, moveTextRight);
      }
    
      public function moveTextRight (e:Event):void {
          if (t.x <= 300) {
            t.x += 10;
            if (t.x > 300) {
              t.x = 300;
            }
          }
      }
    }
}