flash小球碰撞

先新建一个影片剪辑,名称为ball,然后在第一帧的位置加入如下的动作即可
fscommand("fullscreen","true") 

number_of_balls = 4;//可能会引起问题发生的!建议这里最大数为3。我提供的演示有问题了。
speed_limit = 2;//速度的设置。

for (x=1; x<=number_of_balls; x++) {
 //ball = attachMovie("ball", "ball_"+x, _root.getNextHighestDepth(), {_x:Math.random()*650-50, _y:Math.random()*350-50});
 
 //每个球点击事件,打开网页
if(x==1){
	ball = attachMovie("ball", "ball_"+x, _root.getNextHighestDepth(), {_x:50, _y:50});
	 ball.onPress=function(){
    	getURL("http://www.sina.com.cn","_blank"); 
       }
 }
 if(x==2){
	 ball = attachMovie("ball", "ball_"+x, _root.getNextHighestDepth(), {_x:600, _y:50});
	 ball.onPress=function(){
    	getURL("http://www.csdn.net/?ref=toolbar","_blank"); 
       }
 }
 if(x==3){
	 ball = attachMovie("ball", "ball_"+x, _root.getNextHighestDepth(), {_x:50, _y:350});
	 ball.onPress=function(){
    	getURL("http://www.baidu.com","_blank"); 
       }
 }
 if(x==4){
	 ball = attachMovie("ball", "ball_"+x, _root.getNextHighestDepth(), {_x:600, _y:350});
	 ball.onPress=function(){
    	getURL("http://www.weather.com.cn/html/weather/101210101.shtml","_blank"); 
       }
 }
 ball.mass = 1;
 ball.xspeed = Math.random()*8-4;
 ball.yspeed = Math.random()*8-4;
 ball.onEnterFrame = function() {
  if (this._x<41) {
   this._x = 41;
   this.xspeed *= -1;
  }
  else if (this._x>727) {
   this._x = 727;
   this.xspeed *= -1;
  }
  if (this._y<41) {
   this._y = 41;
   this.yspeed *= -1;
  }
  else if (this._y>391) {
   this._y = 391;
   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 = new_xspeed_2;
 final_xspeed_2 = new_xspeed_1;
 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;
 ball2.yspeed = Math.sin(collisionision_angle)*final_xspeed_2+Math.sin(collisionision_angle+Math.PI/2)*final_yspeed_2;
 //修复2球粘附的bug的代码
 ball._x += ball.xspeed;
  ball._y += ball.yspeed;
   ball2._x += ball2.xspeed;
  ball2._y += ball2.yspeed;
}

_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<=82) {
    manage_bounce(_root["ball_"+x],_root["ball_"+y]);
   }
  }
 }
};


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值