摩擦力模拟
1.构建小球
Ball.as
package
{
import flash.display.Sprite;
public class Ball extends Sprite
{
private var _radius:Number;
private var _color:uint;
public function Ball (radius:Number,color:uint)
{
_radius=radius;
_color=color;
Init ();
}
public function Init ():void
{
graphics.beginFill (_color);
graphics.drawCircle (0,0,_radius);
graphics.endFill ();
}
}
}
package
{
import flash.display.Sprite;
public class Ball extends Sprite
{
private var _radius:Number;
private var _color:uint;
public function Ball (radius:Number,color:uint)
{
_radius=radius;
_color=color;
Init ();
}
public function Init ():void
{
graphics.beginFill (_color);
graphics.drawCircle (0,0,_radius);
graphics.endFill ();
}
}
}
2.构建动画
Friction.as
package
{
import flash.events.Event;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
public class Friction extends Sprite
{
private var vx:Number;
private var vy:Number;
private var friction:Number;
private var ball:Ball;
private var speed:Number;
private var angle:Number;
public function Friction ()
{
Init ();
}
private function Init ():void
{
stage.scaleMode=StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP_LEFT;
ball=new Ball(20,0xffff00);
ball.x=stage.stageWidth/2;
ball.y=stage.stageHeight/2;
vx=Math.random()*20-10;
vy=Math.random()*20-10;
friction=0.95;
addChild (ball);
addEventListener (Event.ENTER_FRAME,Active);
}
private function Active (e:Event):void
{
speed=Math.sqrt(vx*vx+vy*vy);
angle=Math.atan2(vy,vx);
speed*=friction;
vx=Math.cos(angle)*speed;
vy=Math.sin(angle)*speed;
ball.x+=vx;
ball.y+=vy;
}
}
}
package
{
import flash.events.Event;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
public class Friction extends Sprite
{
private var vx:Number;
private var vy:Number;
private var friction:Number;
private var ball:Ball;
private var speed:Number;
private var angle:Number;
public function Friction ()
{
Init ();
}
private function Init ():void
{
stage.scaleMode=StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP_LEFT;
ball=new Ball(20,0xffff00);
ball.x=stage.stageWidth/2;
ball.y=stage.stageHeight/2;
vx=Math.random()*20-10;
vy=Math.random()*20-10;
friction=0.95;
addChild (ball);
addEventListener (Event.ENTER_FRAME,Active);
}
private function Active (e:Event):void
{
speed=Math.sqrt(vx*vx+vy*vy);
angle=Math.atan2(vy,vx);
speed*=friction;
vx=Math.cos(angle)*speed;
vy=Math.sin(angle)*speed;
ball.x+=vx;
ball.y+=vy;
}
}
}