一個(gè)隨機(jī)畫圓弧的效果 有幾個(gè)屬性可調(diào)整 也不多描述了 看看就知道了
點(diǎn)擊這里看演示
貼一下代碼 有興趣的看一下吧 剛加了下注釋
//******************* draw_arc ************************ //******************* 作者:boenlee ******************* //舞臺(tái)元件: //可視區(qū)域元件--mask //移動(dòng)按鈕--up_btn, down_btn, left_btn, right_btn //文本框--angle_txt, time_txt, r_txt, thickness_txt //復(fù)選按鈕--trace_check, leaf_check //重畫按鈕--redraw_btn //庫鏈接元件: //葉子--leaf //*************************************************** //畫小于等于45度的圓弧,任意角度開始 //mc:目標(biāo)mc名稱 //ox:原點(diǎn)x位置 //oy:原點(diǎn)y位置 //r:半徑 //origin_angle:起始角度 //angle:弧的角度,正數(shù)順時(shí)針畫弧,負(fù)數(shù)逆時(shí)針畫弧 //thickness:線的粗細(xì) //rbg:線的顏色 //alpha:線的透明度 //返回:圓弧終點(diǎn)坐標(biāo),終點(diǎn)為角度大的點(diǎn),與angle符號(hào)無關(guān) function draw_small_arc (mc:MovieClip, ox:Number, oy:Number, r:Number, origin_angle:Number, angle:Number, thickness:Number, rbg:Number, alpha:Number):Object { var origin_angle:Number = origin_angle % 360; var angle:Number = angle % 360; origin_angle = angle < 0 ? origin_angle + angle : origin_angle; angle = Math.abs (angle); angle = angle > 45 ? 45 : angle; var ctrl_r:Number = maintain_digit (1 / cosD (angle / 2) * r, 4); var ctrl_sin:Number = maintain_digit (sinD (origin_angle + angle / 2), 4); var ctrl_cos:Number = maintain_digit (cosD (origin_angle + angle / 2), 4); var org_sin:Number = maintain_digit (sinD (origin_angle), 4); var org_cos:Number = maintain_digit (cosD (origin_angle), 4); var achr_sin:Number = maintain_digit (sinD (origin_angle + angle), 4); var achr_cos:Number = maintain_digit (cosD (origin_angle + angle), 4); with (mc) { lineStyle (thickness, rbg, alpha, false, "normal", "none"); moveTo (org_cos * r + ox, org_sin * r + oy); curveTo (ctrl_cos * ctrl_r + ox, ctrl_sin * ctrl_r + oy, achr_cos * r + ox, achr_sin * r + oy); } return {x:achr_cos * r + ox, y:achr_sin * r + oy}; } //畫小于等于360度的圓弧,任意角度開始 //參數(shù)及返回值與draw_small_arc函數(shù)相同 function draw_arc (mc:MovieClip, ox:Number, oy:Number, r:Number, origin_angle:Number, angle:Number, thickness:Number, rbg:Number, alpha:Number):Object { var origin_angle:Number = angle < 0 ? origin_angle + angle : origin_angle; var angle:Number = Math.abs (angle); var times:Number = int (angle / 45); var last_angle:Number = angle % 45; if (times >= 8) { times = 8; last_angle = 0; } for (var i:Number = 0; i < times; i++) { draw_small_arc (mc, ox, oy, r, 45 * i + origin_angle, 45, thickness, rbg, alpha); } return draw_small_arc (mc, ox, oy, r, 45 * times + origin_angle, last_angle, thickness, rbg, alpha); } //小數(shù)保留 //val:要處理數(shù)值 //maintain:保留位數(shù) //返回:處理后的數(shù) function maintain_digit (val:Number, maintain:Number):Number { var multiple:Number = Math.pow (10, Math.abs (maintain)); return Math.round (val * multiple) / multiple; } //以角度為參數(shù)計(jì)算sin值 function sinD (angle:Number):Number { return Math.sin (Math.PI / 180 * angle); } //以角度為參數(shù)計(jì)算cos值 function cosD (angle:Number):Number { return Math.cos (Math.PI / 180 * angle); } //隨機(jī)顏色 function rnd_color ():Number { return random (255) << 16 | random (255) << 8 | random (255); } //弧線顏色數(shù)組,由于隨機(jī)顏色比較丑,所以選擇幾個(gè)比較好的顏色 var color_arr:Array = [0xFF1111, 0xEDB83D, 0xEBEB1D, 0x99E91F, 0x20E91F, 0x23E4E3, 0x1DC2EB, 0x1B45ED, 0x6F1BED, 0xC21BED, 0xEB1DEB, 0xF017AF, 0xF01784, 0xFFFFFF]; //葉子顏色數(shù)組,原因同上 var leaf_color_arr:Array = [0x25EB25, 0xF3F347, 0x4AF09D, 0xF2AD48]; //起始角度 var origin_angle:Number; //弧的角度 var angle:Number; //弧度是否隨機(jī) var angle_rnd:Boolean; //方向 var dir:Number; //上一個(gè)弧的方向 var old_dir:Number; //弧的個(gè)數(shù) var time:Number; //弧所在圓的坐標(biāo) var posx:Number; var posy:Number; //弧終點(diǎn)位置 var pos_obj:Object; //半徑 var r:Number; //弧線粗細(xì) var thickness:Number; //是否跟蹤 var is_trace:Boolean = false; //是否有樹葉生長效果 var leaf_grow:Boolean = false; //用于畫弧線的mc var line_mc:MovieClip; //初始化屬性 function init_prop () { line_mc.removeMovieClip (); line_mc = this.createEmptyMovieClip ("line_mc", 1); line_mc.setMask (mask); line_mc._x = mask._x; line_mc._y = mask._y; origin_angle = random (4) * 90; angle = get_txt_value (angle_txt, 0, 0, 10, 360); if (angle == 0) { angle_rnd = true; } else { angle_rnd = false; } dir = random (2) ? 1 : -1; posx = random (100) - 50; posy = random (100) - 50; time = get_txt_value (time_txt, 50, 10, 10, 200); r = get_txt_value (r_txt, 20, 5, 5, 50); thickness = get_txt_value (thickness_txt, 5, 1, 1, 10); } //主函數(shù) function main () { if (--time > 0) { dir = random (2) ? 1 : -1; origin_angle += angle; //如果當(dāng)前弧的方向與前一個(gè)的不同,計(jì)算當(dāng)前弧所在圓的原點(diǎn)位置 if (dir + old_dir == 0) { posx += cosD (origin_angle) * 2 * r; posy += sinD (origin_angle) * 2 * r; origin_angle += 180; } if (angle_rnd) { angle = dir * (random (50) + 50); } else { angle = dir * Math.abs (angle); } pos_obj = draw_arc (line_mc, posx, posy, r, origin_angle, angle, thickness, color_arr[random (color_arr.length)], 100); //加載樹葉 if (leaf_grow) { line_mc.attachMovie ("leaf", "leaf" + time, time, {_x:pos_obj.x, _y:pos_obj.y, _rotation:random (360), _xscale:random (50) + r * 2 + thickness * 5, _yscale:random (50) + r * 2 + thickness * 5, _alpha:random (100) + thickness * 5}); var leaf_color:Color = new Color (line_mc["leaf" + time]); leaf_color.setRGB (leaf_color_arr[random (leaf_color_arr.length)]); } //跟蹤 if (is_trace) { line_mc._x = mask._x - pos_obj.x; line_mc._y = mask._y - pos_obj.y; } old_dir = dir; } else { delete line_mc.onEnterFrame; } } //獲取文本框內(nèi)容,為數(shù)值 //txt_name:文本框名稱 //nan_val:文本內(nèi)容非數(shù)字時(shí)所取值 //zero_val:文本內(nèi)容為0時(shí)所取值 //min_val:文本內(nèi)容最小值 //max_val:文本內(nèi)容最大值 function get_txt_value (txt_name:TextField, nan_val:Number, zero_val:Number, min_val:Number, max_val:Number) { if (isNaN (Number (txt_name.text))) { txt_name.text = String (nan_val); } else if (Number (txt_name.text) == 0) { txt_name.text = String (zero_val); } else if (Number (txt_name.text) < min_val) { txt_name.text = String (min_val); } else if (Number (txt_name.text) > max_val) { txt_name.text = String (max_val); } return Number (txt_name.text); } //重畫 redraw_btn.onRelease = function () { delete line_mc.onEnterFrame; init_prop (); line_mc.onEnterFrame = main; }; //選擇是否跟蹤 trace_check.onRelease = function () { is_trace = !is_trace; this.gotoAndStop (Number (is_trace) + 1); }; //選擇是否生長樹葉 leaf_check.onRelease = function () { leaf_grow = !leaf_grow; this.gotoAndStop (Number (leaf_grow) + 1); }; var keep_moving:MovieClip = this.createEmptyMovieClip ("keep_moving", 2); var speed:Number = 10; //移動(dòng)line_mc function moving (speedx:Number, speedy:Number) { keep_moving.onEnterFrame = function () { line_mc._x += speedx; line_mc._y += speedy; }; } //停止移動(dòng) function stop_move () { delete keep_moving.onEnterFrame; } //按鈕控制 up_btn.onPress = function () { moving (0, speed); }; down_btn.onPress = function () { moving (0, -speed); }; left_btn.onPress = function () { moving (speed, 0); }; right_btn.onPress = function () { moving (-speed, 0); }; up_btn.onRelease = up_btn.onReleaseOutside = down_btn.onRelease = down_btn.onReleaseOutside = left_btn.onRelease = left_btn.onReleaseOutside = right_btn.onRelease = right_btn.onReleaseOutside = function () { stop_move (); }; //鍵盤控制 var key_lis:Object = new Object (); key_lis.onKeyDown = function () { switch (Key.getCode ()) { case 37 : moving (speed, 0); break; case 38 : moving (0, speed); break; case 39 : moving (-speed, 0); break; case 40 : moving (0, -speed); break; } }; key_lis.onKeyUp = function () { stop_move (); }; Key.addListener (key_lis); init_prop (); line_mc.onEnterFrame = main;
經(jīng)典論壇交流:
本文鏈接:http://www.95time.cn/download/product/2008/5508.asp
出處:藍(lán)色理想
責(zé)任編輯:moby
|