Mouse drag issue when out of boundary
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!";
}
}
