topical media & game development
animation-ch11-Billiard2.ax
animation-ch11-Billiard2.ax
[swf]
[flash]
flex
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
public class @ax-animation-ch11-Billiard2 extends Sprite
{
private var ball0:animation_ch11_Ball;
private var ball1:animation_ch11_Ball;
public function @ax-animation-ch11-Billiard2()
{
init();
}
private function init():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
ball0 = new animation_ch11_Ball(40);
ball0.mass = 2;
ball0.x = 50;
ball0.y = stage.stageHeight / 2;
ball0.vx = 1;
addChild(ball0);
ball1 = new animation_ch11_Ball(25);
ball1.mass = 1
ball1.x = 300;
ball1.y = stage.stageHeight / 2;
ball1.vx = -1;
addChild(ball1);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
ball0.x += ball0.vx;
ball1.x += ball1.vx;
var dist:Number = ball1.x - ball0.x;
if(Math.abs(dist) < ball0.radius + ball1.radius)
{
var vxTotal:Number = ball0.vx - ball1.vx;
ball0.vx = ((ball0.mass - ball1.mass) * ball0.vx + 2 * ball1.mass * ball1.vx) / (ball0.mass + ball1.mass);
ball1.vx = vxTotal + ball0.vx;
ball0.x += ball0.vx;
ball1.x += ball1.vx;
}
}
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.