Actionscript 3,拖动时旋转对象

发布于 2024-11-09 19:44:08 字数 670 浏览 0 评论 0原文

我有一个需要通过单击和拖动来旋转的对象。按照一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮华 2024-11-16 19:44:08

我看到的问题是 MOUSE_DOWN 事件每次点击仅触发一次,因此您只需在处理程序中运行代码一次。

可能有比这更好的方法,但这就是我考虑这样做的方式:

编辑详细信息:

public class Test extends MovieClip {
    private var n:Needle;

    public function Test() {
        // constructor code
        n = new Needle();

        stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownF,false,0,true);
        stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpF,false,0,true);


        n.x = stage.stageWidth/2; //center needle on stage
        n.y = stage.stageHeight/2;
        addChild(n); //add needle to stage
    }
    public function mouseDownF(e:MouseEvent):void {
        stage.addEventListener(MouseEvent.MOUSE_MOVE,rotate,false,0,true);
    }
    public function rotate(e:MouseEvent):void {
        var angle:Number = Math.atan2(mouseY - n.y,mouseX - n.x); //get angle in radians (pythagoras) 

        angle = angle * 180/Math.PI -90; //convert to degrees , the 90 is to have it point to the mouse

        n.rotation = angle; //rotate
    }
    public function mouseUpF(e:MouseEvent):void {
        stage.removeEventListener(MouseEvent.MOUSE_MOVE,rotate);
    }
}

因此,当用户单击(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:

public class Test extends MovieClip {
    private var n:Needle;

    public function Test() {
        // constructor code
        n = new Needle();

        stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownF,false,0,true);
        stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpF,false,0,true);


        n.x = stage.stageWidth/2; //center needle on stage
        n.y = stage.stageHeight/2;
        addChild(n); //add needle to stage
    }
    public function mouseDownF(e:MouseEvent):void {
        stage.addEventListener(MouseEvent.MOUSE_MOVE,rotate,false,0,true);
    }
    public function rotate(e:MouseEvent):void {
        var angle:Number = Math.atan2(mouseY - n.y,mouseX - n.x); //get angle in radians (pythagoras) 

        angle = angle * 180/Math.PI -90; //convert to degrees , the 90 is to have it point to the mouse

        n.rotation = angle; //rotate
    }
    public function mouseUpF(e:MouseEvent):void {
        stage.removeEventListener(MouseEvent.MOUSE_MOVE,rotate);
    }
}

So when the user clicks down (mouseDown) it activates an event listener that fires the rotate handler every time the mouse moves. When the user lets go of the click the event listener is destroyed. The false,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文