正在阅读:Flash打造漂亮随机画圆弧效果Flash打造漂亮随机画圆弧效果

2008-03-26 09:12 出处:蓝色理想 作者:boenlee 责任编辑:chenzhenjia

  一个随机画圆弧的效果,有几个属性可调整,也不多描述了,看看就知道了:


效果图


  贴一下代码,有兴趣的看一下吧,刚加了下注释...

  //******* 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;

关注我们

最新资讯离线随时看 聊天吐槽赢奖品