利用flash Actionscript对小球运动进行管理,并且实现对碰撞的检测的实例代码。
启动Flash 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;
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);
上一篇:
黑客、红客、蓝客、飞客究竟是什么?
下一篇:
Photoshop使普通照片变得具有视觉冲击效果