亲, 转载请附本文链接哦~
package poolandlane
{
import flash.events.MouseEvent;
import flash.geom.Point;
import mx.core.UIComponent;
/**
* 组件移动监听工具类
* @author Admin
*
*/
public class DragElement
{
private var downPoint:Point;
private var elem:UIComponent;
private static var instance:DragElement;
public function DragElement()
{
if(instance != null)
{
throw new Error("instance is not null");
}
instance = this;
}
public static function getInstance():DragElement
{
if(instance == null)
{
instance = new DragElement();
}
return instance;
}
public function addDragEvent(ui:UIComponent):void
{
elem = ui;
ui.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
}
private function mouseDownHandler(event:MouseEvent):void
{
downPoint = new Point(event.localX, event.localY);
elem.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
elem.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
}
protected function mouseMoveHandler(event:MouseEvent):void
{
elem.x += event.localX - downPoint.x;
elem.y += event.localY - downPoint.y;
event.stopPropagation();
}
private function mouseUpHandler(event:MouseEvent):void
{
elem.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
elem.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
event.stopPropagation();
}
}
}
使用方法:
DragElement.getInstance().addDragEvent(想要移动的组件id);