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]);}}}
};
總結
- 上一篇: K750通过电脑共享宽带上网详细操作步骤
- 下一篇: 平板的android版本是5.0.2,安