A-A+
flash N个小球碰撞效果制作
flash 多个小球进行碰撞的效果感觉很好玩,当然碰撞的效果也是需要使用 as 代码来完成的,只是觉得好玩,暂时还没想到用在什么地方,新手可以拿 as 代码来测试一下,整个 as 代码如下:
- m._visible = 0;
- function setMc(m_width,w,h,speed) {
- stage_w = w-m_width;
- stage_h = h-m_width;
- for (i=1; i<11; i++) {
- M = m.duplicateMovieClip("m"+i, i);
- M_x = m_width/2+random(stage_w);
- M_y = m_width/2+random(stage_h);
- if (i == 1) {
- M._x = M_x;
- M._y = M_y;
- }
- if (i>1) {
- for (j=1; j<i; j++) {
- if (Math.sqrt((this["m"+j]._x-M_x)*(this["m"+j]._x-M_x)+(this["m"+j]._y-M
- _y)*(this["m"+j]._y-M_y))<m_width) {
- i--;
- break;
- }
- }
- if (j == i) {
- M._x = M_x;
- M._y = M_y;
- }
- }
- //以上这些是做小球在一定范围内不重合的随机排列
- M.rotation = random(2*Math.PI);
- M.xspeed = speed;
- M.yspeed = speed;
- //初始每个小球的速度,角度
- M.onEnterFrame = function() {
- this._x += this.xspeed*Math.cos(this.rotation);
- this._y += this.yspeed*Math.sin(this.rotation);
- //--------------
- for (i=1; i<11; i++) {
- if (i == this._name.substr(1, this._name.length)) {
- continue;
- } else if (Math.sqrt((this._y-_root["m"+i]._y)*(this._y-_root["m"+i]._y)+(t
- his._x-_root["m"+i]._x)*(this._x-_root["m"+i]._x))<=m_width) {
- this.rotation = Math.atan2(this._y-_root["m"+i]._y, this._x-_root["m"+i]._x);
- this.xspeed = speed;
- this.yspeed = speed;
- this._x += this.xspeed*Math.cos(this.rotation);
- this._y += this.yspeed*Math.sin(this.rotation);
- break;
- }
- //每个小球对场景内的其他所有小球进行接触判断,这里的接触用的是距离判断
- }
- //---------------
- if (this._x<=m_width/2) {
- this._x = m_width/2+.1;
- this.xspeed *= -1;
- } else if (this._x>=300-m_width/2) {
- this._x = 300-m_width/2-.1;
- this.xspeed *= -1;
- } else if (this._y<=m_width/2) {
- this._y = m_width/2+.1;
- this.yspeed *= -1;
- } else if (this._y>=300-m_width/2) {
- this._y = 300-m_width/2-.1;
- //这里-.1的处理是为了防止了边界出现问题,你可以删掉这句看看,原因还是自己动脑想
- this.yspeed *= -1; }
- };
- //这里是对小球碰到边界的判断
- }
- }
- setMc(m._width,300,300,5);//参数分别是小球的宽度