Flash AS3 custome Drag and drop using MOUSE_MOVE event

I am creating a grid based mesh rendering in AS3 that loads the required PNG image chunks and displays them in a container. I have applied the scroll / drag logic in this application using the MOUSE_MOVE event handler. What am I doing,

I register the bDragging flag in MOUSE_DOWN and the position of the mouse. In each MOUSE_MOVE I check the mouse movement and move the main map accordingly. Unregister the bDragging flag in MOUSE_UP and set the position to NaN.

My problem is that the drag is pretty jerky / wobbly.

Any suggestion will be greatly appreciated. following example code i am using.

function onMouseDown(e:MouseEvent):void
{
     m_bDragging = true;
     m_ptPrevPoint = new Point(e.stageX, e.stageY);
}

function onMouseUp(e:MouseEvent):void
{
     m_bDragging = false;
     m_ptPrevPoint = null;
}
function onMouseMove(e:MouseEvent):void
{
    if(!m_bDragging || null == m_ptPrevPoint)
    return;

var nDiffX:Number = int(e.stageX - m_ptPrevPoint.x);
var nDiffY:Number = int(e.stageY - m_ptPrevPoint.y);

//Make movement smoother 
//nDiffX = nDiffX * 4) / 4;
//nDiffY = nDiffY * 4) / 4;

if(nDiffX != 0 || nDiffY != 0)
{
    trace("X : " + nDiffX + ", Y : " + nDiffY + ", points-Old " + m_ptPrevPoint + ", New " + new Point(e.stageX, e.stageY) );
    m_oCircle.x += nDiffX;
    m_oCircle.y += nDiffY;

    m_ptPrevPoint = new Point(e.stageX, e.stageY);

    e.updateAfterEvent();
}
else
    trace("not moved - X : " + nDiffX + ", Y : " + nDiffY+ ", points-Old " + m_ptPrevPoint + ", New " + new Point(e.stageX, e.stageY)) }

      

Please check the FLA file here ... made in Flash CS3. Note that if the mouse is on the circle it will jerk off like hell, but if you drag it from behind the circle it will become smoother!

FLA example

+2


source to share


2 answers


Thanks guys, I figured out the problem!

actually it might be some kind of adobe bug, I was getting some odd coordinates strangely and it was for this reason that the movement was unspoken. I converted stageX and stageY using localToGlobal and everything is smoother.

now, i dont know why i need to convert stageX / stageY to global. :) my short changes as below.

var curPt: Point = DisplayObject (e.currentTarget) .localToGlobal (new point (e.stageX, e.stageY));



var nDiffX: Number = int (curPt.x - m_ptPrevPoint.x);

var nDiffY: Number = int (curPt.y - m_ptPrevPoint.y);

Thanks guys, every help was appreciated.

+2


source


Does my answer help with this?

Drag and Drop Replication Issues with Mouse Events



Basically, when you move the mouse, the coordinates of the mouse can sometimes be in a different context as expected, depending on the purpose of the event. You need to translate coordinates.

0


source







All Articles