弹性距离碰撞
1.构建小球精灵
Ball.as
package
{
import flash.display.Sprite;
public class Ball extends Sprite
{
public var radius:Number;
public var color:uint;
public var vx:Number=0;
public var vy:Number=0;
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.构建碰撞动画
package
{
import flash.display.Sprite;
public class Ball extends Sprite
{
public var radius:Number;
public var color:uint;
public var vx:Number=0;
public var vy:Number=0;
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 ();
}
}
}
Bubbles.as
package
{
import flash.events.Event;
import flash.display.Sprite;
public class Bubbles extends Sprite
{
private var balls:Array;
private var bigBall:Ball;
private var bounce:Number=-0.7;
private var numBalls:Number=10;
private var spring:Number=0.2;
public function Bubbles ()
{
Init ();
}
private function Init ():void
{
balls=new Array();
bigBall=new Ball(100,0xFF0000);
addChild (bigBall);
bigBall.x=stage.stageWidth/2;
bigBall.y=stage.stageHeight/2;
for (var i:int=0; i<numBalls; i++)
{
var ball=new Ball(Math.random()*40+5,0xffcc00);
addChild (ball);
ball.x=Math.random()*stage.stageWidth;
ball.y=Math.random()*stage.stageHeight;
ball.vx=Math.random()*6-3;
ball.vy=Math.random()*6-3;
balls.push (ball);
}
addEventListener (Event.ENTER_FRAME,EnterFrame);
}
private function EnterFrame (e:Event):void
{
for (var i:int=0; i<numBalls; i++)
{
var ball:Ball=balls[i];
Move (ball);
var dx:Number=ball.x-bigBall.x;
var dy:Number=ball.y-bigBall.y;
var dist:Number=Math.sqrt(dx*dx+dy*dy);
var minDist:Number=ball.radius+bigBall.radius;
if (dist<minDist)
{
var angle:Number=Math.atan2(dy,dx);
var tx:Number=bigBall.x+Math.cos(angle)*minDist;
var ty:Number=bigBall.y+Math.sin(angle)*minDist;
ball.vx+=(tx-ball.x)*spring;
ball.vy+=(ty-ball.y)*spring;
}
}
}
private function Move(ball:Ball):void
{
ball.x+=ball.vx;
ball.y+=ball.vy;
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;
}
}
}
}
package
{
import flash.events.Event;
import flash.display.Sprite;
public class Bubbles extends Sprite
{
private var balls:Array;
private var bigBall:Ball;
private var bounce:Number=-0.7;
private var numBalls:Number=10;
private var spring:Number=0.2;
public function Bubbles ()
{
Init ();
}
private function Init ():void
{
balls=new Array();
bigBall=new Ball(100,0xFF0000);
addChild (bigBall);
bigBall.x=stage.stageWidth/2;
bigBall.y=stage.stageHeight/2;
for (var i:int=0; i<numBalls; i++)
{
var ball=new Ball(Math.random()*40+5,0xffcc00);
addChild (ball);
ball.x=Math.random()*stage.stageWidth;
ball.y=Math.random()*stage.stageHeight;
ball.vx=Math.random()*6-3;
ball.vy=Math.random()*6-3;
balls.push (ball);
}
addEventListener (Event.ENTER_FRAME,EnterFrame);
}
private function EnterFrame (e:Event):void
{
for (var i:int=0; i<numBalls; i++)
{
var ball:Ball=balls[i];
Move (ball);
var dx:Number=ball.x-bigBall.x;
var dy:Number=ball.y-bigBall.y;
var dist:Number=Math.sqrt(dx*dx+dy*dy);
var minDist:Number=ball.radius+bigBall.radius;
if (dist<minDist)
{
var angle:Number=Math.atan2(dy,dx);
var tx:Number=bigBall.x+Math.cos(angle)*minDist;
var ty:Number=bigBall.y+Math.sin(angle)*minDist;
ball.vx+=(tx-ball.x)*spring;
ball.vy+=(ty-ball.y)*spring;
}
}
}
private function Move(ball:Ball):void
{
ball.x+=ball.vx;
ball.y+=ball.vy;
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;
}
}
}
}