as3: ActionQueue

[ 2009-08-16 22:50:47 | Author: liuhuan ]
Font Size: Large | Medium | Small
ActionQueue is a useful class executing actions in queue. You can download it at: http://www.libspark.org/svn/as3/ActionQueue/ActionQueue.as


package {
  import flash.display.*;
  import flash.events.*;

  public class Main extends Sprite {
    var aq:ActionQueue;
    public function Main():void {
      aq = new ActionQueue();
      initAction();
      aq.execute();
    }
    public function initAction() {
      aq.addAction( this, drawing, ["circle"], this, "moveComplete");
      aq.addAction( this, drawing, ["rect"], this, "moveComplete");
      aq.addAction( this, initAction);
    }
    private function drawing(type:String = "rect"):void {
      var shape:Shape = new Shape();
      shape.graphics.clear();
      shape.graphics.beginFill(Math.random() * 256);
      switch (type) {
        case "rect" :
          shape.graphics.drawRect(0,0,Math.random()*40+5,Math.random()*40+5);
          break;
        case "circle" :
          shape.graphics.drawCircle(0,0,Math.random()*30+5);
          break;
        default :
          break;
      }
      shape.graphics.endFill();
      shape.x = Math.random()*200-100+stage.stageWidth/2;
      shape.y = Math.random()*200-100+stage.stageHeight/2;
      addChild(shape);
      shape.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
    }
    private function onEnterFrameHandler(e:Event):void {
      e.target.x += (stage.stageWidth/2 - e.target.x)*.1;
      e.target.y += (stage.stageHeight/2 - e.target.y)*.1;
      e.target.alpha -= .01;
      if (e.target.alpha<= 0) {
        this.dispatchEvent(new Event("moveComplete"));
        e.target.removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
      }
    }
  }
}
[Last Modified By liuhuan, at 2009-08-16 22:20:16]
Comments Feed Comments Feed: http://www.liuhuan.com/blog/feed.asp?q=comment&id=1010

There is no comment on this article.

You can't post comment on this article.