| 首页 | 新闻 | 网页 | 设计 | 色彩 | 原创 | 视觉 | 素材 | 动漫 | 酷站 | 策划 | 文案 | 访谈 | 运营 | 编程 | 数据库 | 服务器 | 下载 | 图库 | 
您的位置: 幽幽天空 > 网页 > 网页制作 > Flash教程 > ActionScript教程 > 文章正文 用户登录
从名片看到一个小
中国互联网还有机
做图片站电影站赚
做图片站电影站赚
flash内置类Sound
浅谈应用按钮事件
一定范围内随机排
运用for循环来对多
小球的移动和缩放
跟随鼠标的小球

多个小球进行碰撞检测           

多个小球进行碰撞检测

作者:佚名 来源:闪吧 作者: ycccc8202 更新:2007-1-13 20:49:46 错误报告 我要投稿

这里是做个简单的测试而已: 效果如下:
点击浏览该文件 整个的代码如下:
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)+(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);//参数分别是小球的宽度,
原文件如下:
点击浏览该文件 //小球多了会有些卡
文章录入:skyuu    责任编辑:skyuu 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    发表评论:
    姓名:  评 分: 1分 2分 3分 4分 5分
     
  • 严禁发表危害国家安全、政治、黄色淫秽等内容的评论。
  • 用户需对自己在使用幽幽天空服务过程中的行为承担法律责任。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表机友个人观点,与本网站立场无关。