How to drag an image to move it smoothly on screen, with ActionScript?

前端 未结 4 1057
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-20 02:48

I was thinking this is a simple task, but I\'m wrong.

I used a sprite to display an image, and when user drag it(MOUSE_DOWN and MOUSE_MOVE), I got the position in

4条回答
  •  渐次进展
    2021-01-20 03:23

    You must use a high framerate to have smooth movement. The optimal framerate is 60fps because it is the default for most LCD monitors.

    I don't have the code under my hand so I put the code here (not tested)

    yourClip.addeventListenner(MouseEvent.MOUSE_DOWN, startDrag);
    
    function startDrag(e:MouseEvent = null):void{
        // record positon of the mouse relative to the clip
        deltaX=stage.mouseX-yourClip.x;
        deltaY=stage.mouseY-yourClip.y;
        // attach on mouse move event
        yourClip.addeventListenner(MouseEvent.MOUSE_MOVE, updateDrag);
        // attach stop event (on Stage)
        Stage.addeventListenner(MouseEvent.MOUSE_UP, stopDrag);
        }
    function updateDrag(e:MouseEvent = null):void{
        yourClip.x=stage.mouseX-deltaX
        yourClip.y=stage.mouseY-deltaY
    }
    
    function stopDrag(e:MouseEvent = null):void{
       yourClip.removeEventListenner(MouseEvent.MOUSE_MOVE, updateDrag);
       stage.removeEventListenner(MouseEvent.MOUSE_UP, stopDrag);
    }
    

提交回复
热议问题