精灵简单缓动效果
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.构建缓动动画
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 ();
}
}
}
Earsing.as
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
public class Earsing extends Sprite
{
private var ball:Ball;
private var earsing:Number=0.2;
private var targetX:Number=stage.stageWidth/2;
private var targetY:Number=stage.stageHeight/2;
public function Earsing ()
{
Init ();
}
public function Init ():void
{
ball=new Ball(20,0xFF0000);
ball.x=0;
ball.y=0;
addChild (ball);
ball.addEventListener (MouseEvent.MOUSE_DOWN,MouseDown);
addEventListener (Event.ENTER_FRAME,EnterFrame);
}
public function MouseDown (e:MouseEvent):void
{
stage.addEventListener (MouseEvent.MOUSE_UP,MouseUp);
removeEventListener (Event.ENTER_FRAME,EnterFrame);
ball.startDrag ();
}
public function MouseUp (e:MouseEvent):void
{
stage.removeEventListener (MouseEvent.MOUSE_UP,MouseUp);
addEventListener (Event.ENTER_FRAME,EnterFrame);
ball.stopDrag ();
}
public function EnterFrame (e:Event):void
{
var dx:Number=targetX-ball.x;
var dy:Number=targetY-ball.y;
var distance:Number=Math.sqrt(dx*dx+dy*dy);
//停止缓动
if (distance<1)
{
targetX=ball.x;
targetY=ball.y;
removeEventListener (Event.ENTER_FRAME,EnterFrame);
}
else
{
ball.x+=dx*earsing;
ball.y+=dy*earsing;
}
}
}
}
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
public class Earsing extends Sprite
{
private var ball:Ball;
private var earsing:Number=0.2;
private var targetX:Number=stage.stageWidth/2;
private var targetY:Number=stage.stageHeight/2;
public function Earsing ()
{
Init ();
}
public function Init ():void
{
ball=new Ball(20,0xFF0000);
ball.x=0;
ball.y=0;
addChild (ball);
ball.addEventListener (MouseEvent.MOUSE_DOWN,MouseDown);
addEventListener (Event.ENTER_FRAME,EnterFrame);
}
public function MouseDown (e:MouseEvent):void
{
stage.addEventListener (MouseEvent.MOUSE_UP,MouseUp);
removeEventListener (Event.ENTER_FRAME,EnterFrame);
ball.startDrag ();
}
public function MouseUp (e:MouseEvent):void
{
stage.removeEventListener (MouseEvent.MOUSE_UP,MouseUp);
addEventListener (Event.ENTER_FRAME,EnterFrame);
ball.stopDrag ();
}
public function EnterFrame (e:Event):void
{
var dx:Number=targetX-ball.x;
var dy:Number=targetY-ball.y;
var distance:Number=Math.sqrt(dx*dx+dy*dy);
//停止缓动
if (distance<1)
{
targetX=ball.x;
targetY=ball.y;
removeEventListener (Event.ENTER_FRAME,EnterFrame);
}
else
{
ball.x+=dx*earsing;
ball.y+=dy*earsing;
}
}
}
}