Skip to main content
Participating Frequently
April 24, 2010
Question

Mouse drag issue when out of boundary

  • April 24, 2010
  • 1 reply
  • 350 views

In one of my tasks for my flash project I am working on, I prompt a user to drag an orb/marble across one end of a tube into another. The functionality is all working correctly, except for one issue I have tried and tried to find a fix for. If the user starts dragging the marble, then raises the mouse above/below or anyway outside of the boundary, the mouse continues to move the object as the mouse is moved (in this case, only horizontally) if this makes any sense.

What I want is for the marble to not move and stop dragging if the mouse is moved outside of the boundary. My code for this frame is listed below:

stop();
next_btn.visible = false;

//remove Child instances from previous frame
removeChild(hearts_mc);
removeChild(diamonds_mc);
removeChild(clubs_mc);
removeChild(spades_mc);

marble_mc.buttonMode = true;

marble_mc.addEventListener(MouseEvent.MOUSE_DOWN, moveMarble);
marble_mc.addEventListener(MouseEvent.MOUSE_UP, dropMarble);
marble_mc.addEventListener(MouseEvent.MOUSE_MOVE, dragMarble);

reply_txt.text ="Drag the marble to the right side of the tube.";

function moveMarble(event:MouseEvent):void {
    //set boundaries of Rectangle (x,y,width,height)
    event.target.startDrag(true, new Rectangle(54,184,486,0));
}

function dropMarble(event:MouseEvent):void {
    event.target.stopDrag();
}

function dragMarble(event:MouseEvent):void {
    //check if marble has reached right side of tube
    if (marble_mc.x >= 540){
        event.target.stopDrag();
      
    //remove event listeners after task is complete
    event.target.removeEventListener(MouseEvent.MOUSE_DOWN, moveMarble);
    event.target.removeEventListener(MouseEvent.MOUSE_UP, dropMarble);
    event.target.removeEventListener(MouseEvent.MOUSE_MOVE, dragMarble);
      
    next_btn.visible = true;
    reply_txt.text ="Great job!";
    marble_mc.buttonMode = false;
       } else if (marble_mc.x > 54 && marble_mc.x <= 539 ){
        reply_txt.text ="Keep going!";
       }
}

This topic has been closed for replies.

1 reply

ClockmortAuthor
Participating Frequently
April 24, 2010

Hm, I think i solved my own problem after staring at it a bit longer (my head is probably spinning circles at this point). My changes made are in bold.

stop();
next_btn.visible = false;

//remove Child instances from previous frame
removeChild(hearts_mc);
removeChild(diamonds_mc);
removeChild(clubs_mc);
removeChild(spades_mc);

marble_mc.buttonMode = true;

marble_mc.addEventListener(MouseEvent.MOUSE_DOWN, moveMarble);
marble_mc.addEventListener(MouseEvent.MOUSE_UP, dropMarble);
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragMarble);

reply_txt.text ="Drag the marble to the right side of the tube.";

function moveMarble(event:MouseEvent):void {
    //set boundaries of Rectangle (x,y,width,height)
    event.target.startDrag(true, new Rectangle(54,184,486,0));
}

function dropMarble(event:MouseEvent):void {
    event.target.stopDrag();
}

function dragMarble(event:MouseEvent):void {
    var xCoord:Number = stage.mouseX
    var yCoord:Number = stage.mouseY

    //track x/y coordinates of mouse on stage
    //trace(xCoord);
    //trace(yCoord);
    if (xCoord <= 26 || yCoord <= 152 || yCoord >= 209){
        this.stopDrag();
    }
    //ch
eck if marble has reached right side of tube
    if (marble_mc.x >= 540){
        this.stopDrag();
      
    //remove event listeners after task is complete
    event.target.removeEventListener(MouseEvent.MOUSE_DOWN, moveMarble);
    event.target.removeEventListener(MouseEvent.MOUSE_UP, dropMarble);
    event.target.removeEventListener(MouseEvent.MOUSE_MOVE, dragMarble);
      
    next_btn.visible = true;
    reply_txt.text ="Great job!";
    marble_mc.buttonMode = false;
       } else if (marble_mc.x > 54 && marble_mc.x <= 539 ){
        reply_txt.text ="Keep going!";
       }
}