2006/07/03 | 曲线运动的实现
类别(Flash课件设计) | 评论(1) | 阅读(332) | 发表于 21:09
折线段运动完成后,就在想曲线怎么做呢?曲线命令中怎么计算出控制点坐标?又怎样限制曲线不超出舞台范围呢?
想了一下午(当然不是坐在电脑前想),突然想到,既然控制点不容易计算,干脆就以折线的端点作为曲线的控制点,控制点的中点位置作为曲线的起始点,不就很容易的解决了上面的几个问题吗?示意图如下:



图片如下:


具体的代码如下,由折线运动的代码演化而来,改动代码只用了半小时时间。
this._x = this._y = 0;
total = 5;
myColor = random(0xffffff);
speed = 5;
for (i = 0; i < total; i++) {
  createEmptyMovieClip("mc" + i, i);
  this["mc" + i]._y = random(Stage.height);
  this["mc" + i]._x = random(Stage.width);
  this["mc" + i].xsp = random(speed) + 5;
  this["mc" + i].ysp = random(speed) + 5;
}
onEnterFrame = function () {
  r = Math.floor(random(5));
  g = Math.floor(random(10));
  b = Math.floor(random(20));
  //myColor += r << 32 + g << 16 + b;
  //myColor &= 0xffffff;
  for (i = 0; i < total; i++) {
    with (this["mc" + i]) {
      _x = _x + xsp;
      _y = _y + ysp;
      if (_x > Stage.width || _x < 0) {
        xsp = -xsp;
        myColor += r << 32 + g << 16 + b;
        myColor &= 0xffffff;
      }
      if (_y > Stage.height || _y < 0) {
        ysp = -ysp;
        myColor += r << 32 + g << 16 + b;
        myColor &= 0xffffff;
      }
    }
  }
  createEmptyMovieClip("xian", 100);
  with (xian) {
    lineStyle(2, myColor, 100);
    start_pt_x = (mc0._x + this["mc" + (total - 1)]._x) / 2;
    start_pt_y = (mc0._y + this["mc" + (total - 1)]._y) / 2;
    moveTo(start_pt_x, start_pt_y);
    for (i = 0; i < total - 1; i++) {
      ctl_pt_x = this["mc" + i]._x;
      ctl_pt_y = this["mc" + i]._y;
      end_pt_x = (this["mc" + (i + 1)]._x + this["mc" + i]._x) / 2;
      end_pt_y = (this["mc" + (i + 1)]._y + this["mc" + i]._y) / 2;
      curveTo(ctl_pt_x, ctl_pt_y, end_pt_x, end_pt_y);
    }
    ctl_pt_x = this["mc" + (total - 1)]._x;
    ctl_pt_y = this["mc" + (total - 1)]._y;
    end_pt_x = (this["mc0"]._x + this["mc" + (total - 1)]._x) / 2;
    end_pt_y = (this["mc0"]._y + this["mc" + (total - 1)]._y) / 2;
    curveTo(ctl_pt_x, ctl_pt_y, end_pt_x, end_pt_y);
  }
};

代码看上去会有些晕,先在示意图上摸拟一下电脑的画图,就可以较容易的理解程序的流程。以上代码写在一个MC元件中,向舞台上拖上四个该元件,效果如下所示:

Flash 动画
0

评论Comments