Chapter 34 Example 2

by actionscriptbible
♥0 | Line 56 | Modified 2010-02-09 02:11:37 | MIT License
play

ActionScript3 source code

/**
 * Copyright actionscriptbible ( http://wonderfl.net/user/actionscriptbible )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/nMAV
 */

package {
  import flash.display.Shape;
  import flash.display.Sprite;
  import flash.events.TimerEvent;
  import flash.geom.Matrix;
  import flash.utils.Timer;

  public class ch34ex2 extends Sprite {
    protected var s1:Shape;
    protected var s2:Shape;
    protected var moves:Array;
    protected var moveNum:int;
    protected var timer:Timer;
    
    public function ch34ex2() {
      s1 = new Shape();
      s2 = new Shape();
      s1.graphics.beginFill(0xff0000, 0.5);
      s1.graphics.drawRect(20, 20, 100, 50);
      s1.graphics.endFill();
      s2.graphics.beginFill(0x0000ff, 0.5);
      s2.graphics.drawRect(20, 20, 100, 50);
      s2.graphics.endFill();
      addChild(s1);
      addChild(s2);
      
      var m:Matrix;
      moves = new Array();

      m = new Matrix();
      m.translate(200, 0);
      moves.push(m);
      
      m = new Matrix();
      m.scale(1, 3);
      moves.push(m);
      
      m = new Matrix();
      m.rotate(Math.PI/4);
      moves.push(m);
      
     
      moveNum = 0;
      timer = new Timer(1500);
      timer.addEventListener(TimerEvent.TIMER, nextMove);
      timer.start();
    }
    protected function nextMove(event:TimerEvent):void {
      var m:Matrix;
      var i:int;
      
      i = moveNum;
      m = s1.transform.matrix;
      m.concat(moves[i]);
      s1.transform.matrix = m;
      
      i = moves.length - moveNum - 1;
      m = s2.transform.matrix;
      m.concat(moves[i]);
      s2.transform.matrix = m;
      
      if (++moveNum >= moves.length) {
        timer.stop();
      }
    }
  }
}