1.构建小球精灵
Ball.as
package
{
import flash.display.Sprite;
public class Ball extends Sprite
{
public var radius:Number;
public var color:uint;
public function Ball (radius:Number,color:uint)
{
this.radius=radius;
this.color=color;
Init ();
}
public function Init ():void
{
graphics.beginFill (color);
graphics.drawCircle (0,0,radius);
graphics.endFill ();
}
}
}
2.构建碰撞跟踪
ObjectHitTest.as
package
{
import flash.display.Sprite;
import flash.events.Event;
public class ObjectHitTest extends Sprite
{
private var ballA:Ball;
private var ballB:Ball;
public function ObjectHitTest ()
{
Init ();
}
private function Init ():void
{
ballA=new Ball(40,0xFF0000);
addChild (ballA);
ballA.x=stage.stageWidth/2;
ballA.y=stage.stageHeight/2;
ballB=new Ball(40,0xFFCC00);
addChild (ballB);
ballB.x=mouseX;
ballB.y=mouseY;
ballB.startDrag ();
addEventListener (Event.ENTER_FRAME,EnterFrame);
}
private function EnterFrame (e:Event):void
{
if (ballA.hitTestObject(ballB))
{
trace ("Hi,Hit.");
}
}
}
}
此方法是简单的碰撞测试,是对物体的边界盒进行测试,所以很不准确。