AS3如何设置鼠标向下拖动的边界?

时间:2011-06-27 04:32:29

标签: actionscript-3

我是AS3的新手,我有一个比舞台(200w)大的正方形(1200w)。现在你可以尽可能地左右拖动它。如何设置限制/边界可以拖动多少平方?那么它不能超出它的最大宽度?

Here's an image

this.addEventListener(MouseEvent.MOUSE_DOWN, 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);
}

请注意我不想使用startdrag()方法。

1 个答案:

答案 0 :(得分:0)

根据您的图片,假设mc指的是蓝色框。

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;
}

希望这就是你所追求的。