how to check if the user drags the object right or left side in actionscript 3

advertisements

I am creating a flash based application, where user can change a rectangle shapes, width and height using mouse drag. here is a quick prototype image.

let me explain briefly: In the image you can see, i am having a tiny red rectangle, which one now sitting is starting position and user can drag that only 100px to right side. The idea is when, user is dragging that to right, i want to rectangle also expand right side with that, like a flexible box. if he is dragging back then it will return with that.

so, the doubt is: how will check, whether user is dragging right side or left side. so based on that we can update the rectangle width.

Here is the code :

import flash.geom.Rectangle;
import flash.events.MouseEvent;

var horizRect:Rectangle = new Rectangle(scrollPathHoriz.x, scrollPathHoriz.y, 100, 0);
var horizCount:Number;

//event listener for the anchor point.
scrollHoriz.addEventListener(MouseEvent.MOUSE_DOWN, dragScroller);
stage.addEventListener(MouseEvent.MOUSE_UP, dropScroller);

//mouse down and mouse up event handler.
function dragScroller(evt:MouseEvent):void {

    horizCount= scrollHoriz.x;

    scrollHoriz.startDrag(false, horizRect);
    scrollHoriz.addEventListener(MouseEvent.MOUSE_MOVE, calculateHorizPixel);
}

function dropScroller(evt:MouseEvent):void {
    scrollHoriz.stopDrag();
    scrollHoriz.removeEventListener(MouseEvent.MOUSE_MOVE, calculateHorizPixel);
}

function calculateHorizPixel(evt:MouseEvent):void {

    horizCount ++;
    trace(horizCount);

}


Since you are already saving the starting x position,

You simply need the difference between starting position & current position :

function calculateHorizPixel(evt:MouseEvent):void {

      var dx = scrollHoriz.x - horizCount;

      trace(dx);
}

A negative value of dx indicates that scrollHoriz moved left, else right.