X

AS树

这是一个AS写的动态树效果,如果你看不到这个flash,请到文章页面查看!

今天在群里有朋友发来个算法,用代码画树,可牛了,修改了贴过来,O(∩_∩)O~ ;-)

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;

    /**
     * www.litefeel.com
     * lite3@qq.com
     * @author lite3
     */    [SWF(width="600", height="400", backgroundColor="0xC7EDCC", frameRote="30")]
    public class ASTree extends Sprite
    {
        private var arr:Array = [];
        private var canvas:Sprite;
        private var pauseFrames:int = 0;
        private var showTree:Function;

        public function ASTree() 
        {
            canvas = this;

            drawTree();
            this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
            stage.addEventListener(MouseEvent.CLICK, clickHandler);

            var txt:TextField = new TextField();
            txt.text = "点击切换效果!";
            addChild(txt);
        }

        private function clickHandler(e:MouseEvent):void 
        {
            drawTree();
        }

        /**
         * 划线
         * @param   beginX
         * @param   beginY
         * @param   endX
         * @param   endY
         */        private function drawLine(beginX:Number, beginY:Number, endX:Number, endY:Number):void
        {
            canvas.graphics.moveTo(beginX, beginY);
            canvas.graphics.lineTo(endX, endY); 
        }

        /**
         * 递归画树
         * @param   x       <b> Number  </b> 基点x    
         * @param   y       <b> Number  </b> 基点y
         * @param   l       <b> Number  </b> 长度
         * @param   angle   <b> Number  </b> 角度
         * @param   n       <b> int     </b> 一个枝的节数
         */        private function lzh(x:Number, y:Number, l:Number, angle:Number, n:int):void
        {
            if (n <= 0) return;

            var a_l:Number, a_r:Number, x1:Number, y1:Number, x2:Number, y2:Number;
            x1 = x + 0.5 * l * Math.cos(angle * Math.PI / 180);
            y1 = y - 0.5 * l * Math.sin(angle * Math.PI / 180);
            x2 = x + l * Math.cos(angle * Math.PI / 180);
            y2 = y - l * Math.sin(angle * Math.PI / 180);
            arr.push([x, y, x2, y2]);
            a_l = angle + 30;
            a_r = angle-30;
            l = l * 2 / 3;
            lzh(x2, y2, l, angle-Math.random() * 10, n - 1);
            lzh(x1, y1, l * 2 / 3, a_l, n - 1);
            lzh(x1, y1, l * 2 / 3, a_r, n - 1);
            lzh(x2, y2, l * 2 / 3, a_l, n - 1);
            lzh(x2, y2, l * 2 / 3, a_r, n - 1);
        }

        private function enterFrameHandler(e:Event):void 
        {
            if (arr.length >0)
            {
                showTree();
            }else if (++pauseFrames > 60)
            {
                pauseFrames = 0;
                drawTree();
            }
        }

        // 重新画树
        private function drawTree():void
        {
            canvas.graphics.clear();
            canvas.graphics.lineStyle(1, Math.random() * 0xFFFFFF, 1);
            arr = [];
            pauseFrames = 0;

            lzh(250, 350, 120, 90, 6);
            showTree = [showTree1, showTree2, showTree3, showTree4][(Math.random() * 4) >> 0] as Function;
        }

        // 画树的效果1
        private function showTree1():void
        {
            for (var i:int = 0; i < 5; i++)
            {
                var arg:Array = arr.pop()
                if (arg) drawLine(arg[0], arg[1], arg[2], arg[3]);
            }
        }
        // 画树的效果2
        private function showTree2():void
        {
            for (var i:int = 0; i < 5; i++)
            {
                var arg:Array = arr.shift()
                if (arg) drawLine(arg[0], arg[1], arg[2], arg[3]);
            }
        }
        // 画树的效果3
        private function showTree3():void
        {
            for (var i:int = 0; i < 5; i++)
            {
                var index:int = Math.random() * arr.length / 10;
                var arg:Array = arr.splice(index, 1)[0] as Array;
                if (arg) drawLine(arg[0], arg[1], arg[2], arg[3]);
            }
        }
        // 画树的效果4
        private function showTree4():void
        {
            for (var i:int = 0; i < 5; i++)
            {
                var index:int = Math.random() * arr.length / 2;
                var arg:Array = arr.splice(index, 1)[0] as Array;
                if (arg) drawLine(arg[0], arg[1], arg[2], arg[3]);
            }
        }
    }
}

This post was last modified on 2019 年 03 月 04 日 01:15

View Comments (9)

This website uses cookies.