tailedmouse
tailedmouse

Reputation: 365

Need to move this Object in one direction

I am trying to move this seatBelt object just on the horizontal plane. I have posted what I did so far. it's not really working out because it seems like once I press the button the seat belt is only able to move towards the right side and MOUSE_UP doesn't work. can somebody guide me on how to go about doing this?

 seatBelt.addEventListener (MouseEvent.MOUSE_DOWN, prsing);
    seatBelt.addEventListener (MouseEvent.MOUSE_UP, lfting);
    seatBelt.addEventListener (MouseEvent.MOUSE_MOVE, mving);




    function prsing (e:MouseEvent){
    posX= seatBelt.x ;
        posY = seatBelt.y ;
        mouseclick = 1;


    }
    function mving (e:MouseEvent) {
        if (mouseclick == 1) {
        seatBelt.x = mouseX ;
        }
    }

    function lfting (e:MouseEvent){
        seatBelt.x = posX;
        seatBelt.y = posY;
        mouseclick =0;

    }

Upvotes: 0

Views: 461

Answers (3)

mfa
mfa

Reputation: 5087

You can use the Sprite.startDrag method to do what you need. No MOUSE_MOVE listener is needed. Lookup the params if you need to: Sprite. I like to only have only one of the MOUSE_DOWN or MOUSE_UP listeners active at a time.

seatBelt.addEventListener (MouseEvent.MOUSE_DOWN, prsing);

function prsing (e:MouseEvent){
    seatBelt.startDrag(false, new Rectangle(0,seatBelt.y,stage.width,seatBelt.y));
    seatBelt.removeEventListener (MouseEvent.MOUSE_DOWN, prsing);
    seatBelt.addEventListener (MouseEvent.MOUSE_UP, lfting);
}

function lfting (e:MouseEvent){
    seatBelt.stopDrag();
    seatBelt.addEventListener (MouseEvent.MOUSE_DOWN, prsing);
    seatBelt.removeEventListener (MouseEvent.MOUSE_UP, lfting);
}

Upvotes: 0

mitim
mitim

Reputation: 3201

What you have mostly looks okay code-wise, except what I think is happening is that you are "mousing up" on the stage/somewhere off the seatBelt object. I'm guessing this as to make the object move you have it reacting to mouse moving...to which I am guessing you are moving the mouse all over the stage and off the seatbelt object at which point you are then releasing the mouse.

To check if this is the case, try releasing the mouse over your seatBelt object to see if your MOUSE_UP event does get triggered.

I'm guessing the behaviour you want is that you want to have the object stop moving at any point regardless of where the mouse is released. To do this, try adding the MOUSE_UP event listener to the stage:

this.stage.addEventListener (MouseEvent.MOUSE_UP, lfting);

Though, since you may not always want this listener to be active, perhaps only add it when the mouse is pressed down over the object by adding and removing the listener only as needed.

I've edited your code a bit to show what I mean and taken out the "mouseclick" boolean as it wouldn't really be needed to keep track of the mouse down/up event anymore:

seatBelt.addEventListener(MouseEvent.MOUSE_DOWN, prsing);

function prsing(e:MouseEvent){
    posX= seatBelt.x ;
    posY = seatBelt.y ;
    this.stage.addEventListener(MouseEvent.MOUSE_MOVE, mving);
    this.stage.addEventListener(MouseEvent.MOUSE_UP, lfting);
}

function mving(e:MouseEvent) {
    seatBelt.x = mouseX;
}

function lfting(e:MouseEvent){
    seatBelt.x = posX;
    seatBelt.y = posY;
    this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mving);
    this.stage.removeEventListener(MouseEvent.MOUSE_UP, lfting);
}

Upvotes: 0

Allan
Allan

Reputation: 3314

So your intended functionality is to be able to drag the seatBelt along the x-axis and on release for it to go back to its original position?

You need to change the MOUSE_UP and MOUSE_MOVE to listen to the stage rather than the seatBelt. This is because it is possible for you to release the button when it is no longer over the seatBelt and so the function never gets called. The stage however will receive the event.

stage.addEventListener(MouseEvent.MOUSE_UP, lifting);
stage.addEventListener(MouseEvent.MOUSE_MOVE, moving);

I am not sure where you are declaring the mouseX variable but if you are after dragging functionality alter the listener to:

   function moving (e:MouseEvent) {
        if (mouseclick == 1) {
        seatBelt.x = e.stageX;
        }
    } 

Upvotes: 1

Related Questions