设为首页 加入收藏 网站搜索 繁體中文 中国建站网 — 站长资源分享平台

多个小球进行碰撞检测

来源:136z.Com 作者:佚名 时间:2005-07-19 00:21:02

这里是做个简单的测试而已: 效果如下:
点击浏览该文件

整个的代码如下:
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; jif (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))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)+(this._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);//参数分别是小球的宽度,
原文件如下:
点击浏览该文件 //小球多了会有些卡

Tags:

  • 好的评价 如果您觉得好,就请您
      0%(0)
  • 差的评价 如果您觉得差,就请您
      0%(0)
  • 相关文章
    广告赞助

    文章随便看看 设计素材 建站学院 网页模板 视频教程

    网友评论

    共有 0 位网友发表了评论,得分 0 分,平均 0 分    查看完整评论

    用户名: 查看更多评论

    分 值:100分 85分 70分 55分 40分 25分 10分 1分

    内 容:

             通知管理员 验证码: