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
    评论
Jupyter Notebook是一个交互式的计算环境,它结合了代码、文本和可视化元素,常用于数据科学和机器学习项目。如果你想在Jupyter Notebook中模拟小碰撞,通常会用到Python的matplotlib库来绘制场景,加上物理模拟库如Pygame或NumPy进行运动和碰撞检测。 小碰撞模拟涉及到基本的物理学概念,如力、速度、加速度和碰撞规则(弹性碰撞、非弹性碰撞或完全非弹性碰撞)。以下是一个简单的步骤概述: 1. 导入所需库:首先需要导入matplotlib用于绘图,以及一个用于物理模拟的库,例如`pygame`。 ```python import pygame from pygame.locals import * ``` 2. 初始化游戏窗口和小:设置窗口大小,创建两个或更多小对象,存储它们的位置、速度和大小等信息。 3. 渲染循环:在主循环中,更新每个小的位置,检查它们是否发生碰撞,然后重新绘制场景。 ```python while True: # 更新小位置 for ball in balls: ball.update() # 检查碰撞 for i, ball1 in enumerate(balls): for j, ball2 in enumerate(balls): if i != j and check_collision(ball1, ball2): handle_collision(ball1, ball2) # 重绘屏幕 screen.fill((0, 0, 0)) for ball in balls: draw_ball(screen, ball) pygame.display.flip() ``` 4. 碰撞处理函数:定义函数来处理碰撞,根据碰撞类型调整小的速度或反弹。 5. 结束条件:添加退出游戏的条件,比如按下特定键或达到预定帧数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值