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

前端 未结 4 1077
佛祖请我去吃肉
佛祖请我去吃肉 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:05

    If you're looking for smooth dragging experience, here is a method you could use: (let's say the thing you're dragging is called 'dragee' and this code is in the scope of dragee's parent)

    function startDragging():void {
        dragee.addEventListener(Event.ENTER_FRAME,dragUpdate);
    }
    
    function stopDragging():void {
        dragee.removeEventListener(Event.ENTER_FRAME,dragUpdate);
    }
    
    var decay:Number = .25; //1 is no decay, .1 would be cazy slow
    
    function dragUpdate(e:Event):void {
        dragee.x += decay * (mouseX - dragee.x);
        dragee.y += decay * (mouseY - dragee.y);
    }
    

提交回复
热议问题