Hi,
I am creating grid based map renderer in AS3 which loads required chunks of PNG images and render them in a container. I've applied scrolling/dragging logic in this app using MOUSE_MOVE event handler. What I do is,
I register bDragging flag in MOUSE_DOWN and position of mouse, In every MOUSE_MOVE, I check displacement of mouse and move main map accordingly Unregister bDragging flag in MOUSE_UP and set position to NaN.
My problem is dragging is quite jerky/shaky.
Any suggestion would be appreciated. following is sample code I'm 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 FLA file here...made in Flash CS3. Please note if mouse is on circle it will jerk like hell but if you drag it from outside of circle, It will go smoother!