Flash / Flex / ActionScript/TextField/Animation

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

Animating a TextField horizontally to x-coordinate 300

   <source lang="java">

package {

 import flash.display.*;
 import flash.events.*;
 import flash.text.*;
 public class Main extends Sprite {
   private var t:TextField = new TextField(  );
   public function Main (  ) {
     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;
       }
     } else {
       removeEventListener(Event.ENTER_FRAME, moveTextRight);
     }
   }
 }

}

       </source>
   
  


Animating a TextField horizontally to x-coordinate 300, timer version

   <source lang="java">

package {

 import flash.display.*;
 import flash.events.*;
 import flash.utils.*;
 import flash.text.*;
 public class Main extends Sprite {
   private var t:TextField = new TextField(  );
   private var timer:Timer;
   public function Main(  ) {
     t.text          = "Hello";
     t.autoSize      = TextFieldAutoSize.LEFT;
     addChild(t);
     timer = new Timer(50, 0);
     timer.addEventListener(TimerEvent.TIMER, moveTextRight);
     timer.start(  );
   }
   public function moveTextRight (e:TimerEvent):void {
     if (t.x <= 300) {
       t.x += 10;
       if (t.x > 300) {
         t.x = 300;
       }
       e.updateAfterEvent(  );  // Update the screen following this function
     } else {
       timer.stop(  );
     }
   }
 }

}

       </source>
   
  


Frame Rate"s Effect on Event.ENTER_FRAME Animations

   <source lang="java">

package {

 import flash.display.*;
 import flash.events.*;
 import flash.utils.*;
 import flash.text.*;
 public class Main extends Sprite {
   private var startMsg:TextField = new TextField(  );
   public function Main(  ) {
     var timer:Timer = new Timer(3000, 1);
     timer.addEventListener(TimerEvent.TIMER, timerListener);
     timer.start(  );
     startMsg.autoSize = TextFieldAutoSize.LEFT;
     startMsg.text = "Get Ready!";
     addChild(startMsg);
   }
   private function timerListener (e:TimerEvent):void {
     startMsg.text = "GO!";
   }
 }

}

       </source>
   
  


Timer event

   <source lang="java">

package {

 import flash.display.TextField;
 import flash.util.Timer;
 import flash.events.*;
 public class BlinkText extends TextField {
   private var timer:Timer;
   public function BlinkText (delay:Number = 1000) {
     timer = new Timer(delay, 0);
     timer.addEventListener(TimerEvent.TIMER, timerListener);
     timer.start(  );
   }
   private function timerListener (e:TimerEvent):void {
     visible = !visible;
     e.updateAfterEvent(  );
   }
   public function setDelay (newDelay:Number):void {
     timer.delay = newDelay;
   }
   public function startBlink (  ):void {
     timer.start(  );
   }
   public function stopBlink (  ):void {
     visible = true;
     timer.stop(  );
   }
 }

}

       </source>
   
  


To eliminate unnecessary function calls, we unregister moveTextRight( ) for Event.ENTER_FRAME events.

   <source lang="java">

package{

   import flash.display.*;
   import flash.text.*;
   import flash.events.*;
   public class Main extends Sprite {
     private var t:TextField = new TextField(  );
   
     public function Main (  ) {
       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;
           }
         } else {
           removeEventListener(Event.ENTER_FRAME, moveTextRight);
         }
   
     }
   }

}

       </source>