AS3 How to set boundries to Mouse Down drag?
I'm new to AS3 and I have a square(1200w) that's bigger than the stage(200w). Right now you can keep dragging it left and right as far as you possibly can. How can I set a limit/boundry to how much of the square you can drag? So that it can't be dragged beyond it's maximum width?
Here's an image
this.addEventListener(MouseEvent.MOUSE_DO开发者_开发知识库WN, mouseDownHandler);
this.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
function mouseDownHandler(e:MouseEvent) {
this.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}
function mouseMoveHandler(e:MouseEvent) {
square_mc.x = mouseX;
}
function mouseUpHandler(e:MouseEvent) {
removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}
Please note I do not want to use the startdrag()
method.
Based on your image, assume that mc
refers to the blue box.
var ox:Number = 0;
mc.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(e:MouseEvent):void
{
ox = mc.mouseX;
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
}
function mouseUpHandler(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}
function mouseMoveHandler(e:MouseEvent):void
{
mc.x = mc.parent.mouseX - ox;
if(mc.x > 0) mc.x = 0;
if(mc.x + mc.width < stage.stageWidth) mc.x = stage.stageWidth - mc.width;
}
Hopefully this is what you were after.
精彩评论