I want to replicate the standard startDrag/stopDrag events with my own routine to alter things a bit, and I run into some kind of event propagation or bubbling problem. Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
mouseDown="mouseDown = true" mouseUp="mouseDown = false"
mouseMove="mouseMove(event)">
<mx:Script>
<![CDATA[
private var mouseDown:Boolean = false;
private var oldMouseX:int = 0, oldMouseY:int = 0;
private function mouseMove(e:MouseEvent):void {
if (mouseDown) {
object.x += (e.localX - oldMouseX);
object.y += (e.localY - oldMouseY);
}
oldMouseX = e.localX;
oldMouseY = e.localY;
trace(e.localX);
}
]]>
</mx:Script>
<mx:Label id="object" text="Drag me" />
</mx:Application>
The problem with this code is that as you drag the object to the right, you will see in the trace that occasionally some random localX values arrive there, resulting in the object jerking from side to side.
I don't understand how to fix that part, I do think it's the label which bubbles the mousemove event, but I don't understand how to stop it from doing that.
Any suggestions are greatly appreciated!
Track which target you're concerned about and only listen to events directly from it. So in your mousemove function, check e.target == your object. In this case, the application. You will get stray events bubbling from sub components.