Actionscript 3,拖动时旋转对象
我有一个需要通过单击和拖动来旋转的对象。按照一些 AS2 代码,每次单击鼠标时,我都会让对象旋转一点,但无法使其与拖动一起工作。
needle.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_2);
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_2);
function fl_ClickToDrag_2(event:MouseEvent):void
{
var angle = Math.atan2(mouseY-needle.y,mouseX-needle.x);
// apply rotation to handle by converting angle into degrees
needle.rotation = angle*180/Math.PI;
// rotate the grip opposite the handle so it won't rotate along with it
//this.grip._rotation = -this._rotation;
}
function fl_ReleaseToDrop_2(event:MouseEvent):void
{
needle.stopDrag();
}
I have an object I need to rotate by clicking and dragging. Following some AS2 code I got the object to rotate a bit every time the mouse is clicked, but can't get it to work with drag.
needle.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_2);
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_2);
function fl_ClickToDrag_2(event:MouseEvent):void
{
var angle = Math.atan2(mouseY-needle.y,mouseX-needle.x);
// apply rotation to handle by converting angle into degrees
needle.rotation = angle*180/Math.PI;
// rotate the grip opposite the handle so it won't rotate along with it
//this.grip._rotation = -this._rotation;
}
function fl_ReleaseToDrop_2(event:MouseEvent):void
{
needle.stopDrag();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看到的问题是 MOUSE_DOWN 事件每次点击仅触发一次,因此您只需在处理程序中运行代码一次。
可能有比这更好的方法,但这就是我考虑这样做的方式:
编辑详细信息:
因此,当用户单击(
mouseDown
)时,它会激活一个触发的事件侦听器每次鼠标移动时旋转
处理程序。当用户松开单击时,事件侦听器将被销毁。添加事件监听器时的false,0,true);
是使其成为 弱引用侦听器,以便它被垃圾收集器收集,而不会永远坐在内存中占用空间。Well the problem I see is that the
MOUSE_DOWN
event only fires once per click, so you only run the code in the handler once.There could be a better way than this but this is how I'd consider doing it:
EDITED FOR DETAIL:
So when the user clicks down (
mouseDown
) it activates an event listener that fires therotate
handler every time the mouse moves. When the user lets go of the click the event listener is destroyed. Thefalse,0,true);
when adding the event listener is to make it a weakly referenced listener so that it gets collected by the garbage collector and doesn't just sit in memory taking up space forever.