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]);
}
}
}
}
» 转载请注明来源:www.litefeel.com » 《AS树》
» 本文链接地址:https://www.litefeel.com/as-tree-movie/
» 订阅本站:www.litefeel.com/feed/
» Host on Linode VPS
» 本文链接地址:https://www.litefeel.com/as-tree-movie/
» 订阅本站:www.litefeel.com/feed/
» Host on Linode VPS
太有创意了 下载下来 好好研究下 谢谢楼主啊
@Michael
恩,没意见了,要是能链接到具体的那篇就更好了 :roll: :grin:
你怎么打到我空间转载你的文章?
我留了你的连接,保留了你的author 你应该没意见哦?
O(∩_∩)O哈哈~,就是分形了
难道是分形算法?
呵呵 我对算法实在是不感冒 汗~~~ 学习下
@hay168
不好意思,贴代码的时候,不小心 enterFrameHandler 丢了, 现已更正,多谢支持!~ :razz:
把 MyContextMenu 相关的都注释掉就可以了后还提示
1120: 访问的属性 enterFrameHandler 未定义。兄弟你能否把他相关的都打包帖上呀,我想学一下它的算法,能满足吗.谢谢
@hay168
没关系的,你把 MyContextMenu 相关的都注释掉就可以了 :grin: :| ;-)
无法测试你的ASTree,因为还缺乏你的MyContextMenu,能帖上来吗