0
votes

Im trying to make a simple object move from its original position to one of the corners in which direction the mouse is being moved. But its a bit trickier, because I need the mouse to travel some distance in one of the directions before moving the object in that corners, in which the mouse is being moved ( to be sure that we didnt accidentally moved the mouse/object ).

I was thinking that I have to save the starting point when the mouse has been pressedDown, and after that to check when it(the mouse) has moved XX pixels in one of the directions and then if moved above some point (in which we are sure that it is not a random movement) move the object to one of the corners. But .... I cant quite figure it out howe to be done by code.

To show more clearly the above Ill explain it with this picture enter image description here

So the Gray Box is our object.

The Arrows are the directions in which we can move the box.

The Green line is the distance that we need to move our mouse (while it is pressedDown)so the object moves in one of the directions./in this example 50px/

Lets assume we clicked the Grey Box in the upper right corner(where the filled green circle is), so if we want to move the box to the LEFT, we need at least to move the mouse(while it is still pressedDown) to the position where the left hollow green circle is. And if we want to move the object to the RIGHT, we need to move the mouse to the right hollow green circle, before the Grey Box goes in that corner.

The RED and BLUE circles are just different scenarios, representing different points in which we have clicked the BOX and where we need to move the mouse, so the BOX moves.

:) Any help will be very appreciated.

1
Post the code you've tried. You're correct in that you just need to store the mouse point on MOUSE_DOWN and listen either on ENTER_FRAME or MOUSE_MOVE (better) and check there if the current mouse position has moved far enough from the mouse down point to be considered a move. - BadFeelingAboutThis

1 Answers

1
votes

Here is a simple example:

var grayBox:Sprite; //let's say this is your gray square or whatever you're clicking
grayBox.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);


var mouseDownPoint:Point = new Point(); //the point to capture on mouse down to compare against
var currentObject:DisplayObject; //to store the current object on mouse down
var threshold:int = 50; //how far in pixels the mouse needs to move before you count it as a move
var didItMove:Boolean = false; //stores whether the current click was a move

function mouseDownHandler(e:MouseEvent):void {
    mouseDownPoint.x = e.stageX;
    mouseDownPoint.y = e.stageY;
    currentObject = e.currentTarget as DisplayObject;
    didItMove = false; //reset this
    stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHanlder, false, 0, true); //listen to mouse move until mouse up
    stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
}

function mouseMoveHandler(e:MouseEvent):void {
    if (Math.abs(e.stageX - mouseDownPoint.x) > threshold || Math.abs(e.stageY - mouseDownPoint.y) > threshold) {
        //moved far enough 
        didItMove = true;
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); //remove listener
    }
}

function mouseUpHandler(e:MouseEvent):void {
    if(stage.hasEventListener(MouseEvent.MOUSE_MOVE)) stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); //remove listener if still around
    stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); //remove the mouse up listener from the stage
    if (didItMove) {
        //do something

        //use currentObject 

        if(e.stageX > mouseDownPoint.x:
            if(e.stageY > mouseDownPoint.y){
                //bottom right corner
            }else {
                //top right corner
            }
        }else{
            if(e.stageY > mouseDownPoint.y){
                //bottom left corner
            }else {
                //top left corner
            }
        }
    }

    currentObject = null; //free the var so it's hanging around in memory
}