欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Flash教程:制作随机画圆弧动画

程序员文章站 2022-04-26 15:19:22
一个随机画圆弧的效果 有几个属性可调整 也不多描述了 看看就知道了 点击这里看演示 贴一下代码 有兴趣的看一下吧 刚加了下注释 //******************* draw_arc **********... 08-10-06...
一个随机画圆弧的效果
有几个属性可调整
也不多描述了
看看就知道了

点击这里看演示
贴一下代码
有兴趣的看一下吧
刚加了下注释
//******************* draw_arc ************************
//******************* 作者:boenlee *******************
//舞台元件:
//可视区域元件--mask
//移动按钮--up_btn, down_btn, left_btn, right_btn
//文本框--angle_txt, time_txt, r_txt, thickness_txt
//复选按钮--trace_check, leaf_check
//重画按钮--redraw_btn
//库链接元件:
//叶子--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_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;
//移动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_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;