Flash AS3 custome使用MOUSE_MOVE事件拖动 [英] Flash AS3 custome Dragging using MOUSE_MOVE event
问题描述
我在MOUSE_DOWN中注册bdragging标志,鼠标位置
在每个MOUSE_MOVE中,我检查鼠标的位移并相应地移动主地图
取消注册MOUSE_UP中的bDragging标志,并将位置设置为NaN。
我的问题是拖动是相当干涩/不稳定。
任何建议将不胜感激。以下是我正在使用的示例代码。
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;
}
函数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);
//使运动更平滑
// nDiffX = nDiffX * 4)/ 4;
// nDiffY = nDiffY * 4)/ 4;
if(nDiffX!= 0 || nDiffY!= 0)
{
trace(X:+ nDiffX +,Y:+ nDiffY + -Old+ m_ptPrevPoint +,新建+新点(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))}
请在此查看FLA文件... CS3。请注意,如果鼠标在圆圈上,它会像地狱一样抖动,但是如果从圆圈外拖动它,它会更顺利!
实际上这可能是一些adobe的bug,我奇怪地得到一些奇怪的坐标,这就是原因运动不顺利。我转换stageX和stageY使用localToGlobal,一切都变得更加顺利!!!现在,我不知道为什么我需要将stageX / stageY转换为全局。 :)我的简短的更改如下。
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);
感谢各位,各位帮助赞赏。
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!
Thanks guys, I figured out the problem!
actually this might be some bug of adobe, I was strangely getting some odd coordinates and that was the reason movement was not smooth. I converted stageX and stageY using localToGlobal and everything became smoother!!!
now, I dont know why should I be needed to convert stageX/stageY to global. :) my brief changes are 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 appreciated.
这篇关于Flash AS3 custome使用MOUSE_MOVE事件拖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!