Flash / Flex / ActionScript/Animation/Animation

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

Animating by Frames

 
package
{
    import flash.display.*;
    import flash.events.Event;
    public class Main extends Sprite
    {
        public function FrameAnimation()
        {
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        protected function onEnterFrame(event:Event):void
        {
            trace("animating");
        }
    }
}



Animating with Pure ActionScript

 
package
{
    import flash.display.*;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    public class Main extends Sprite
    {
        protected var myShape:DisplayObject;
        public function Main()
        {
            var fps:int = 60;
            var mspf:int = Math.round(1 / fps * 1000);
            var t:Timer = new Timer(mspf);
            t.addEventListener(TimerEvent.TIMER, onTimer);
            t.start();
        var s:Sprite = new Sprite();
        addChild(s);
            myShape = getChildAt(0);
        }
        protected function onTimer(event:TimerEvent):void
        {
            myShape.x += 0.5;
            event.updateAfterEvent();
        }
    }
}



Animation and Speed

 
package
{
    import flash.display.*;
    import flash.events.Event;
    import flash.utils.getTimer;
    public class Main extends Sprite
    {
        protected const SPEED:Number = 50; //50 pixels per second
        protected var myShape:DisplayObject;
        protected var lastTime:int;
        public function Main ()
        {
        addEventListener(Event.ENTER_FRAME, onEnterFrame);
        lastTime = getTimer();
        var s:Sprite = new Sprite();
        addChild(s);
        myShape = getChildAt(0);
        }
        protected function onEnterFrame(event:Event):void
        {
           var time:int = getTimer();
            myShape.x += SPEED * (time - lastTime) / 1000;
            lastTime = time;
       }
    }
}



A timer function-based animation

 
package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    
    public class Main extends Sprite {
        private var _sprite:Sprite = new Sprite(  );
        private var _vx:Number = 1;
        private var _vy:Number = 1;
        private var _timer:Timer;
        
        public function Main(  ) {
            _sprite.graphics.beginFill(0x0000ff, 100);
            _sprite.graphics.drawCircle(0, 0, 25);
            _sprite.graphics.endFill(  );
            _sprite.x = 50;
            _sprite.y = 100;
            addChild(_sprite);
            _timer = new Timer(30);
            _timer.addEventListener("timer", onTimer);
            _timer.start(  );
        }
        
        public function onTimer(event:TimerEvent):void {
            _sprite.x += _vx;
            _sprite.y += _vy;
        }
    }    
}



Moving an Object

 
package {
    import flash.display.Sprite;
    import flash.events.Event;
    
    public class Main extends Sprite {
        private var _sprite:Sprite = new Sprite(  );
        private var _vx:Number = 3;
        
        public function Main(  ) {
            _sprite.graphics.beginFill(0x0000ff, 100);
            _sprite.graphics.drawCircle(0, 0, 25);
            _sprite.graphics.endFill(  );
            _sprite.x = 50;
            _sprite.y = 10;
            addChild(_sprite);
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        public function onEnterFrame(event:Event):void {
            _sprite.x += _vx;
        }
    }    
}



Programmatic Animation

 
package{
    import flash.display.*;
    import flash.text.*;
    
    public class Main extends Sprite {
      public function Main(  ) {
        var t:TextField = new TextField(  );
        t.text          = "Hello";
        t.autoSize      = TextFieldAutoSize.LEFT;
        addChild(t);
    
        while (t.x <= 300) {
          t.x += 10;
        }
      }
    }
}