拖动精灵的三种方法比较
构建Ball精灵:
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 ();
}
}
}
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 ();
}
}
}
第一种方法:会出现拖动时突然跳跃,使自身的中心与鼠标的中心对齐。
MouseMoveDrag.as
package
{
import flash.events.MouseEvent;
import flash.display.Sprite;
public class MouseMoveDrag extends Sprite
{
private var ball:Ball;
public function MouseMoveDrag()
{
Init();
}
public function Init():void
{
ball=new Ball(40,0xFFFF00);
ball.x=stage.stageWidth/2;
ball.y=stage.stageHeight/2;
addChild(ball);
ball.addEventListener(MouseEvent.MOUSE_DOWN,MouseDown);
}
public function MouseDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP,MouseUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE,MouseMove);
}
public function MouseUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP,MouseUp);
stage.removeEventListener(MouseEvent.MOUSE_MOVE,MouseMove);
}
public function MouseMove(e:MouseEvent):void
{
ball.x=mouseX;
ball.y=mouseY;
}
}
}
package
{
import flash.events.MouseEvent;
import flash.display.Sprite;
public class MouseMoveDrag extends Sprite
{
private var ball:Ball;
public function MouseMoveDrag()
{
Init();
}
public function Init():void
{
ball=new Ball(40,0xFFFF00);
ball.x=stage.stageWidth/2;
ball.y=stage.stageHeight/2;
addChild(ball);
ball.addEventListener(MouseEvent.MOUSE_DOWN,MouseDown);
}
public function MouseDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP,MouseUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE,MouseMove);
}
public function MouseUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP,MouseUp);
stage.removeEventListener(MouseEvent.MOUSE_MOVE,MouseMove);
}
public function MouseMove(e:MouseEvent):void
{
ball.x=mouseX;
ball.y=mouseY;
}
}
}
第二种方法:完全正确,稍微有点繁琐。
MouseMoveDrag.as
package
{
import flash.events.MouseEvent;
import flash.display.Sprite;
public class MouseMoveDrag extends Sprite
{
private var ball:Ball;
private var dx:Number;
private var dy:Number;
public function MouseMoveDrag()
{
Init();
}
public function Init():void
{
ball=new Ball(40,0xFFFF00);
ball.x=stage.stageWidth/2;
ball.y=stage.stageHeight/2;
addChild(ball);
ball.addEventListener(MouseEvent.MOUSE_DOWN,MouseDown);
}
public function MouseDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP,MouseUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE,MouseMove);
dx=ball.x-mouseX;
dy=ball.y-mouseY;
}
public function MouseUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP,MouseUp);
stage.removeEventListener(MouseEvent.MOUSE_MOVE,MouseMove);
}
public function MouseMove(e:MouseEvent):void
{
ball.x=mouseX+dx;
ball.y=mouseY+dy;
}
}
}
package
{
import flash.events.MouseEvent;
import flash.display.Sprite;
public class MouseMoveDrag extends Sprite
{
private var ball:Ball;
private var dx:Number;
private var dy:Number;
public function MouseMoveDrag()
{
Init();
}
public function Init():void
{
ball=new Ball(40,0xFFFF00);
ball.x=stage.stageWidth/2;
ball.y=stage.stageHeight/2;
addChild(ball);
ball.addEventListener(MouseEvent.MOUSE_DOWN,MouseDown);
}
public function MouseDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP,MouseUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE,MouseMove);
dx=ball.x-mouseX;
dy=ball.y-mouseY;
}
public function MouseUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP,MouseUp);
stage.removeEventListener(MouseEvent.MOUSE_MOVE,MouseMove);
}
public function MouseMove(e:MouseEvent):void
{
ball.x=mouseX+dx;
ball.y=mouseY+dy;
}
}
}
第三种方法:简单,但是startDrag()和stopDrag()每次只能拖动一个物体。
MouseMoveDrag.as
package
{
import flash.events.MouseEvent;
import flash.display.Sprite;
public class MouseMoveDrag extends Sprite
{
private var ball:Ball;
public function MouseMoveDrag()
{
Init();
}
public function Init():void
{
ball=new Ball(40,0xFFFF00);
ball.x=stage.stageWidth/2;
ball.y=stage.stageHeight/2;
addChild(ball);
ball.addEventListener(MouseEvent.MOUSE_DOWN,MouseDown);
}
public function MouseDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP,MouseUp);
ball.startDrag();
}
public function MouseUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP,MouseUp);
ball.stopDrag();
}
}
}
package
{
import flash.events.MouseEvent;
import flash.display.Sprite;
public class MouseMoveDrag extends Sprite
{
private var ball:Ball;
public function MouseMoveDrag()
{
Init();
}
public function Init():void
{
ball=new Ball(40,0xFFFF00);
ball.x=stage.stageWidth/2;
ball.y=stage.stageHeight/2;
addChild(ball);
ball.addEventListener(MouseEvent.MOUSE_DOWN,MouseDown);
}
public function MouseDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP,MouseUp);
ball.startDrag();
}
public function MouseUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP,MouseUp);
ball.stopDrag();
}
}
}