我可以跟踪拖动事件吗?

时间:2014-01-04 10:02:18

标签: actionscript-3 flash flex actionscript flex4

我想在某个UIComponent上调用startDrag,然后每次拖动组件的位置发生变化时都会收到事件。这可能吗?我还没有发现任何可能性。

1 个答案:

答案 0 :(得分:1)

我认为你可能想要使用MouseDown,MouseMove和MouseUp。

var myUIComponent
var isDown:Boolean = false;
var startX:Number = 0;
var startY:Number = 0;

myUIComponent.addEventListener(MouseEvent.MOUSE_DOWN, down);

function down(e:MouseEvent){
    startX = e.stageX;
    startY = e.stageY;
    isDown = true;
    stage.addEventListener(MouseEvent.MOUSE_MOVE,moving);
    stage.addEventListener(MouseEvent.MOUSE_UP, up);
}

function moving(e:MouseEvent)
{
    if (isDown){
        var distanceX = startX - e.stageX;
        var distanceY = startY - e.stageY;
        // Do something
    }
}

function up(e:MouseEvent)
{
    stage.removeEventListener(MouseEvent.MOUSE_MOVE,moving);
    stage.removeEventListener(MouseEvent.MOUSE_UP, up);
    isDown = false;
}