返回“Flash基础理论课 - 目录”
编写代码
首先要让两个小球以一定的角度运动并且最后相互碰撞。开始的设置与前面相同,两个小球实例:ball0和ball1。这次让它们变大一点,如图 11-9 所示,这样它们碰撞的机会就会大一些。
图11-9 二维动量守恒,设置舞台
package {
import flash.display.Sprite;
import flash.events.Event;
public class Billiard3 extends Sprite {
private var ball0:Ball;
private var ball1:Ball;
private var bounce:Number = -1.0;
public function Billiard3() {
init();
}
private function init():void {
ball0 = new Ball(150);
ball0.mass = 2;
ball0.x = stage.stageWidth - 200;
ball0.y = stage.stageHeight - 200;
ball0.vx = Math.random() * 10 - 5;
ball0.vy = Math.random() * 10 - 5;
addChild(ball0);
ball1 = new Ball(90);
ball1.mass = 1;
ball1.x = 100;
ball1.y = 100;
ball1.vx = Math.random() * 10 - 5;
ball1.vy = Math.random() * 10 - 5;
addChild(ball1);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void {
ball0.x += ball0.vx;
ball0.y += ball0.vy;
ball1.x += ball1.vx;
ball1.y += ball1.vy;
checkWalls(ball0);
checkWalls(ball1);
}
private function checkWalls(ball:Ball):void {
if (ball.x + ball.radius > stage.stageWidth) {
ball.x = stage.stageWidth - ball.radius;
ball.vx *= bounce;
} else if (ball.x - ball.radius < 0) {
ball.x = ball.radius;
ball.vx *= bounce;
}
if (ball.y + ball.radius > stage.stageHeight) {
ball.y = stage.stageHeight - ball.radius;
ball.vy *= bounce;
} else if (ball.y - ball.radius < 0) {
ball.y = ball.radius;
ball.vy *= bounce;
}
}
}
}