flash教程制造随机画圆弧动画

更新时间:15-10-23 16:02 热度:376
 

一个随机画圆弧的效果
有几个属性可调试
也无几描写了
看看就晓得了

点击这处看演习
贴一下子代码
有兴致的看一下子吧
刚加了下注解
//******************* draw_arc ************************
//******************* 笔者:boenlee *******************
//戏台元件:
//可视地区范围元件–mask
//移动按键–up_变态n, down_变态n, left_变态n, right_变态n
//文本框–angle_txt, time_txt, r_txt, thickness_txt
//复选按键–trace_check, leaf_check
//重画按键–redraw_变态n
//库链接元件:
//叶子–leaf
//***************************************************
//画小于等于45度的圆弧,恣意角度着手
//mc:目的mc名字
//ox:原点x位置
//oy:原点y位置
//r:半径
//origin_angle:开始角度
//angle:弧的角度,正数顺时针画弧,负数逆时针画弧
//thickness:线的粗细
//rbg:线的颜色
//alpha:线的公开度
//回返:圆弧尽头坐标,尽头为角度大的点,与angle符号无关
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度的圆弧,恣意角度着手
//参变量及回返值与draw_small_arc函数相同
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);
}
//小数保存
//val:要处置数字
//maintain:保存位数
//回返:处置后的数
function maintain_digit (val:number, maintain:number):number {
var multiple:number = math.pow (10, math.abs (maintain));
return math.round (val * multiple) / multiple;
}
//以角度为参变量计算sin值
function sind (angle:number):number {
return math.sin (math.pi / 180 * angle);
}
//以角度为参变量计算cos值
function cosd (angle:number):number {
return math.cos (math.pi / 180 * angle);
}
//随机颜色
function rnd_color ():number {
return random (255) << 16 random (255) << 8 random (255);
}
//弧线颜色数组,因为随机颜色比较丑,所以挑选几个比较好的颜色
var color_arr:array = [0xff1111, 0xedb83d, 0xebeb1d, 0x99e91f, 0x20e91f, 0x23e4e3, 0x1dc2eb, 0x1b45ed, 0x6f1bed, 0xc21bed, 0xeb1deb, 0xf017af, 0xf01784, 0xffffff];
//叶子颜色数组,端由同上
var leaf_color_arr:array = [0x25eb25, 0xf3f347, 0x4af09d, 0xf2ad48];
//开始角度
var origin_angle:number;
//弧的角度
var angle:number;
//弧度是否随机
var angle_rnd:boolean;
//方向
var dir:number;
//上一个弧的方向
var old_dir:number;
//弧的个数
var time:number;
//弧存在的地方圆的坐标
var posx:number;
var posy:number;
//弧尽头位置
var pos_obj:object;
//半径
var r:number;
//弧线粗细
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);
}
//主函数
function main () {
if (–time > 0) {
dir = random (2) ? 1 : -1;
origin_angle = angle;
//假如现时弧的方向与前一个的不一样,计算现时弧存在的地方圆的原点位置
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;
}
}
//取得文本框内部实质意义,为数字
//txt_name:文本框名字
//nan_val:文本内部实质意义非数码时所取值
//zero_val:文本内部实质意义为0时所取值
//min_val:文本内部实质意义最小值
//max_val:文本内部实质意义最大值
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_变态n.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;
//移动line_mc
function moving (speedx:number, speedy:number) {
keep_moving.onenterframe = function () {
line_mc._x = speedx;
line_mc._y = speedy;
};
}
//休止移动
function stop_move () {
delete keep_moving.onenterframe;
}
//按键扼制
up_变态n.onpress = function () {
moving (0, speed);
};
down_变态n.onpress = function () {
moving (0, -speed);
};
left_变态n.onpress = function () {
moving (speed, 0);
};
right_变态n.onpress = function () {
moving (-speed, 0);
};
up_变态n.onrelease = up_变态n.onreleaseoutside = down_变态n.onrelease = down_变态n.onreleaseoutside = left_变态n.onrelease = left_变态n.onreleaseoutside = right_变态n.onrelease = right_变态n.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;