动作脚本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();
}
    

解决方法

好吧,我看到的问题是
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,true);
        stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpF,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,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
)时,它会激活一个事件侦听器,该事件侦听器在每次鼠标移动时都会触发
rotate
处理程序。当用户放开单击时,事件侦听器将被销毁。添加事件侦听器时的ѭ5表示使它成为弱引用的侦听器,以便由垃圾回收器收集它,而不仅仅是坐在内存中永久占用空间。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...