递归的简单实现
- package
- {
- import com.FPS;
- import com.Vector2D;
- import flash.display.Graphics;
- import flash.display.Sprite;
- import flash.events.Event;
- import com.Rect;
- import com.ColorUtil;
- import flash.events.MouseEvent;
- import flash.geom.Point;
- /**
- * ...
- * @author ddw1997
- */
- [SWF(width="800",height="600",frameRate="24",backgroundColor="#999999")]
- public class RectTree extends Sprite
- {
- private var rectW:int;
- private var level:int;
- private var speed:Number;
- private var percent:Number;
- private var contain:Sprite;
- private var rect0:Rect;
- private var isLoop:Boolean;
-
- public function RectTree()
- {
- if (stage)
- init();
- else
- addEventListener(Event.ADDED_TO_STAGE, init);
- }
- private function init(e:Event = null):void
- {
- removeEventListener(Event.ADDED_TO_STAGE, init);
- rectW = 100;
- level = 8;
- percent = 0.3;
- speed = 0.008;
- contain = new Sprite();
- addChild(contain );
- contain .x = 300;
- contain.y = 500;
- var fps:FPS = new FPS();
- addChild(fps);
- isLoop = true;
- addEventListener(Event.ENTER_FRAME, loop);
- stage.addEventListener(MouseEvent.CLICK, pause);
- }
-
- private function pause(e:MouseEvent):void
- {
- if (isLoop) removeEventListener(Event.ENTER_FRAME, loop);
- else addEventListener(Event.ENTER_FRAME, loop);
- isLoop = !isLoop;
- }
-
- private function loop(e:Event):void
- {
- contain.graphics.clear();
- percent += speed;
- if (percent > 1 || percent < 0)
- {
- speed *= -1;
- }
- percent = Math.min(1, Math.max(0, percent ));
-
- rect0 = new Rect(new Point(0, 0), new Point(rectW, 0), new Point(rectW, rectW), new Point(0, rectW), percent, 0x992200, contain.graphics);
- drawRect(rect0, 1);
- }
-
- private function drawRect(rect:Rect,n:int):void
- {
- if (n > level) return;
- else
- {
- n = n + 1;
- drawRect(getNextRect(rect.pointA,rect.percentPoint,rect.pointB,ColorUtil.getColor(n,level*3),percent,contain.graphics),n);
- drawRect(getNextRect(rect.percentPoint,rect.pointB,rect.pointA,ColorUtil.getColor(n,level*3),percent,contain.graphics),n);
- }
- }
- private function getNextRect(p1:Point,p2:Point,tagPoint:Point,col:uint,percent:Number,g:Graphics):Rect
- {
- var v_12:Vector2D = new Vector2D();
- v_12.updateVector(true, p1, p2);
- v_12 = v_12.rotate( -90);
- var pointA:Point = v_12.endPoint.clone();
- v_12.startPoint = p2;
- var pointB:Point = v_12.endPoint.clone();
- return new Rect(pointA,pointB,p2,p1,percent,col,g)
-
-
-
-
- }
- }
- }
复制代码
|