在Actionscript 3中拖动并旋转MC

时间:2014-02-04 21:39:01

标签: actionscript-3 flash actionscript rotation drag

我正在尝试将一个动画片段放在AS3文件中,并在有人点击并拖动它时使其顺利旋转。我知道我的代码很接近,但现在不是拖动,而是在点击时移动固定的距离。您可以在此处查看示例:http://server.iconixinc.com/drag/

这是我的代码

const TO_DEGREE:Number = 180/Math.PI;

addEventListener(MouseEvent.MOUSE_DOWN, startRotate, true);
addEventListener(MouseEvent.MOUSE_UP, stopRotate, true);
var maxRotSpeed:Number = .1;
var rotScale:Number = 0.2;

function startRotate(e:MouseEvent):void
{



var dx:int = stage.mouseX - myMc.x;
    var dy:int = stage.mouseY - myMc.y;
    var rot:Number = Math.atan2(dy, dx) * TO_DEGREE;
    var drot = rot - myMc.rotation;

    if(drot < -180) drot += 360;
    if(drot > 180) drot -= 360;

    drot *= rotScale;
    myMc.rotation += drot;
}

function stopRotate(e:MouseEvent) {
myMc.stopDrag();
}

对我可能做错了什么的想法?

1 个答案:

答案 0 :(得分:0)

您实际上并未使用AS3的拖放功能,因此您无需拨打stopDrag。您实际上非常接近,您只想将代码移动到移动侦听器中:

const TO_DEGREE:Number = 180/Math.PI;

addEventListener(MouseEvent.MOUSE_DOWN, startRotate, true);
addEventListener(MouseEvent.MOUSE_UP, stopRotate, true);
var maxRotSpeed:Number = .1;
var rotScale:Number = 0.2;

function startRotate(e:MouseEvent):void
{
    // you want the calculation to occur whenever the mouse moves, 
    // not just when the mouse button is clicked
    addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}

function onMouseMove(e:MouseEvent):void {
    var dx:int = stage.mouseX - myMc.x;
    var dy:int = stage.mouseY - myMc.y;
    var rot:Number = Math.atan2(dy, dx) * TO_DEGREE;
    var drot = rot - myMc.rotation;

    if(drot < -180) drot += 360;
    if(drot > 180) drot -= 360;

    drot *= rotScale;
    myMc.rotation += drot;

}

function stopRotate(e:MouseEvent) {
    // instead of calling stopDrag, you simply remove the move listener
    removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}