AS3 - How to drag an object in a row that is not vertical or horizontal

I would like to drag an object into one line. I already know how to do this in a horizontal or vertical line

This is how i do it

private var handle:Sprite;

private function init():void
{
    handle = new Sprite();
    handle.mouseChildren = false;
    handle.buttonMode = true;
    handle.graphics.beginFill(0xFF0000);
    handle.graphics.drawCircle(0, 0, 5);

    handle.addEventListener(MouseEvent.MOUSE_DOWN, startMove);
    handle.addEventListener(MouseEvent.MOUSE_UP, stopMove); 
}

private function startMove(evt:MouseEvent):void
{               

    var bounds:Rectangle = new Rectangle(0, 0, 100, 1);
    handle.startDrag(false, bounds);

}

private function stopMove(evt:MouseEvent):void
{
    handle.stopDrag();
}

But I want to drag my object into a row that is not horizontal or vertical. For example, I would like to drag an object from the upper left corner to the lower right corner, in a straight line.

I tried to rotate the rectangle of borders, but it seems that you cannot rotate the rectangle.

How to drag an object in a non-vertical (or non-horizontal) row?

Many thanks!

Vincent

+3
source share
1 answer

startdrag . enterframe x/y :

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class Test extends Sprite
    {
        private var handle:Sprite;

        public function Test()
        {
            handle = new Sprite();
            addChild(handle);
            handle.mouseChildren = false;
            handle.buttonMode = true;
            handle.graphics.beginFill(0xFF0000);
            handle.graphics.drawCircle(0, 0, 5);

            handle.addEventListener(MouseEvent.MOUSE_DOWN, startMove);
            addEventListener(MouseEvent.MOUSE_UP, stopMove); 
        }

        private function startMove(evt:MouseEvent):void
        {
            stage.addEventListener(Event.ENTER_FRAME, updateClipPos);
        }

        private function stopMove(evt:MouseEvent):void
        {
            stage.removeEventListener(Event.ENTER_FRAME, updateClipPos);
        }

        private function updateClipPos(e:Event) : void
        {
            if(mouseX < 100)
            {
                handle.x = mouseX;
                handle.y = handle.x;
            }
        }

    }
}
+2

All Articles