用户名: 密码: 验证码:           网站地图 高级搜索 RSS订阅 收藏本站
Google
您的位置:首页>>图形设计>>Flash>>阅读资讯:Flash AS制作小球自由运动和碰撞检测的动画

Flash AS制作小球自由运动和碰撞检测的动画

[ 来源: | 阅读:次 | 更新日期:2007-9-21 18:56:02 | 评论 0 条 | 我要投稿 ]

利用flash Actionscript对小球运动进行管理,并且实现对碰撞的检测的实例代码。

字串8

启动Flash 8,修改文档属性如下图。 字串9

Flash AS制作小球自由运动和碰撞检测的动画 字串9

接着建立影片剪辑,设置如下。 www.yueluo.net

Flash AS制作小球自由运动和碰撞检测的动画 字串5

然后回到主场景中,在第一帧输入下面代码: 字串8

number_of_balls = 6;//可能会引起问题发生的!建议这里最大数为3。我提供的演示有问题了。
speed_limit = 10;//速度的设置。
for (x=1; x<=number_of_balls; x++) {
 ball = attachMovie("ball", "ball_"+x, _root.getNextHighestDepth(), {_x:Math.random()*400-50, _y:Math.random()*300-50});
 ball.collision = 0;
 ball.mass = 1;
 ball.xspeed = Math.random()*8-4;
 ball.yspeed = Math.random()*8-4;
 ball.onEnterFrame = function() {
  if (this._x<15) {
   this._x = 15;
   this.xspeed *= -1;
  }
  else if (this._x>485) {
   this._x = 485;
   this.xspeed *= -1;
  }
  if (this._y<15) {
   this._y = 15;
   this.yspeed *= -1;
  }
  else if (this._y>385) {
   this._y = 385; 字串6
   this.yspeed *= -1;
  }
  if (this.xspeed>speed_limit) {
   this.xspeed = speed_limit;
  }
  if (this.xspeed<speed_limit*-1) {
   this.xspeed = speed_limit*-1;
  }
  if (this.yspeed>speed_limit) {
   this.yspeed = speed_limit;
  }
  if (this.yspeed<speed_limit*-1) {
   this.yspeed = speed_limit*-1;
  }
  this._x += this.xspeed;
  this._y += this.yspeed;
 };
}
function manage_bounce(ball, ball2) {
 dx = ball._x-ball2._x;
 dy = ball._y-ball2._y;
 collisionision_angle = Math.atan2(dy, dx);
 magnitude_1 = Math.sqrt(ball.xspeed*ball.xspeed+ball.yspeed*ball.yspeed);
 magnitude_2 = Math.sqrt(ball2.xspeed*ball2.xspeed+ball2.yspeed*ball2.yspeed);
 direction_1 = Math.atan2(ball.yspeed, ball.xspeed);

月落网


 direction_2 = Math.atan2(ball2.yspeed, ball2.xspeed);
 new_xspeed_1 = magnitude_1*Math.cos(direction_1-collisionision_angle);
 new_yspeed_1 = magnitude_1*Math.sin(direction_1-collisionision_angle);
 new_xspeed_2 = magnitude_2*Math.cos(direction_2-collisionision_angle);
 new_yspeed_2 = magnitude_2*Math.sin(direction_2-collisionision_angle);
 final_xspeed_1 = ((ball.mass-ball2.mass)*new_xspeed_1+(ball2.mass+ball2.mass)*new_xspeed_2)/(ball.mass+ball2.mass);
 final_xspeed_2 = ((ball.mass+ball.mass)*new_xspeed_1+(ball2.mass-ball.mass)*new_xspeed_2)/(ball.mass+ball2.mass);
 final_yspeed_1 = new_yspeed_1;
 final_yspeed_2 = new_yspeed_2;
 ball.xspeed = Math.cos(collisionision_angle)*final_xspeed_1+Math.cos(collisionision_angle+Math.PI/2)*final_yspeed_1;
 ball.yspeed = Math.sin(collisionision_angle)*final_xspeed_1+Math.sin(collisionision_angle+Math.PI/2)*final_yspeed_1;
 ball2.xspeed = Math.cos(collisionision_angle)*final_xspeed_2+Math.cos(collisionision_angle+Math.PI/2)*final_yspeed_2;
字串8

 ball2.yspeed = Math.sin(collisionision_angle)*final_xspeed_2+Math.sin(collisionision_angle+Math.PI/2)*final_yspeed_2;
}
_root.onEnterFrame = function() {
 for (x=1; x<number_of_balls; x++) {
  for (y=x+1; y<=number_of_balls; y++) {
   distance_x = Math.abs(_root["ball_"+x]._x-_root["ball_"+y]._x);
   distance_y = Math.abs(_root["ball_"+x]._y-_root["ball_"+y]._y);
   distance = Math.sqrt(distance_x*distance_x+distance_y*distance_y);
   if (distance<=30 && (_root["ball_"+x].collision == 0 or _root["ball_"+y].collision == 0)) {
    _root["ball_"+x].collision = 1;
    _root["ball_"+y].collision = 1;
    manage_bounce(_root["ball_"+x],_root["ball_"+y]);
   }
   else if (distance>30) {

www.yueluo.net


    _root["ball_"+x].collision = 0;
    _root["ball_"+y].collision = 0;
   }
  }
 }
}; www.yueluo.net

按Ctrl+Enter测试效果如下。

www.yueluo.net

字串9


Tags:flash,AS,碰撞检测
责任编辑:
您的评论
用户名:新注册) 密码: 匿名评论 [所有评论]

·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为