Search code examples
actionscript-3flash-cs6

Is this possible to lock the mouse along a path and if so how?


I want to be able to lock a mouse to move along an arc only. Is this possible? And if so how? I have tried looking on Google and forums. All I have found are locking movieclips to a path. I am using ActionScript 3 in Adobe Flash CS6.


Solution

    • hide the mouse cursor
    • create a custom cursor (like actual cursor)
    • change position of your cursor (use mouse position)

    this code will give you an idea

    Mouse.hide();
    var path:Array=[new Point(0,0),new Point(50,20),new Point(150,40),new Point(250,60),new Point(300,70),new Point(400,80)];
    var myCursor:Sprite=new Sprite();
    myCursor.graphics.beginFill(0);
    myCursor.graphics.drawCircle(0,0,5);
    myCursor.graphics.endFill();
    addChild(myCursor);
    
    stage.addEventListener(MouseEvent.MOUSE_MOVE,refreshCursorPosition);
    function refreshCursorPosition(e:MouseEvent):void{
        if(e.stageX<0 || e.stageX>stage.stageWidth)
            return;
        var pos:int=Math.floor(e.stageX*path.length/stage.stageWidth);
        myCursor.x=path[pos].x;
        myCursor.y=path[pos].y;
    }