开发者

Problem rotating image with MOUSE_MOVE

Hey gang. Stumped on something.

I have a disc I am rotating with the mouse with event.MOUSE_MOVE, like a jog whee开发者_StackOverflowl on some audio equipment. Everything almost works as expected, but the problem I am experiencing is that the disc always jumps to the point where the user clicks on the disc. I need the point on the disc that the user clicks on to remain under the mouse while the user spins the disc but I can't seem to come up with the correct math to make it happen. Here's the code i am using:

var xd = (_knob.x  - _stageRef.stage.mouseX);
var yd = (_knob.y  - _stageRef.stage.mouseY);
var radAngle = Math.atan2(yd, xd);
_knob.rotation = int(radAngle * 360/(Math.PI * 2) - 90);

_knob is a vector circle wrapped in a movieclip, with the circle centered on the movieclip's reg point. _stageRef represents the main stage.

Any help would be awesome. I've scoured the interweb and can't come up with anything.

Thx!


You are setting _knob rotation to the angle between _knob and mouse cursor. So if rotation was 0, and angle 45, rotation becomes 45, therefore it jumps. What you need is measure changes in this angle, not setting it instantly:

var _mouseAngle:Number;

function getMouseAngle():Number
{
    var xd = (_knob.x  - _stageRef.stage.mouseX);
    var yd = (_knob.y  - _stageRef.stage.mouseY);
    return Math.atan2(yd, xd);
}

function onMouseDown(event:MouseEvent):void
{
    _mouseAngle = getMouseAngle();
}

function onMouseMove(event:MouseEvent):void
{
    var newAngle:Number = getMouseAngle();
    _knob.rotation += (newAngle - _mouseAngle) * 180.0 / Math.PI; //EDIT: forgot to convert into degrees
    _mouseAngle = newAngle;
}

(Code not tested)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜