检查 MovieClip 的一部分何时离开舞台

Check when a part of the MovieClip leaves the Stage

我正在使用 AS3 创建一个拖放游戏,我想检查 Movieclip 的一部分何时在屏幕外以将视图移到后面并让用户选择将其放置在何处。

我无法测试 MovieClip 凭据是否大于舞台 (scaleMode = NO_SCALE) Width/Height,因为舞台的一部分隐藏在浏览器后面 window.

这和MOUSE_LEAVE是一样的,只是这次它必须是MovieClips,我试图查看MOUSE_LEAVE背后的代码,但我无法找到它。

谢谢。

主要CLASS

[SWF(width='800', height='800',backgroundColor='#CC99FF', frameRate='60')]
public class DragTest extends Sprite
{
    public function DragTest()
    {
        addChild(new World(this));

        this.stage.scaleMode = "noScale";
        this.stage.align = "TL";

        this.graphics.lineStyle(5,0x555555,0.5);
        this.graphics.drawRect(0,0,800,800);
    }
}

世界CLASS

public class World extends Container // Container from my SWC
{
    private var _display:Sprite;
    private var _dragPt:Point;
    private var _dragedObject:MovieClip;

    public function World(display:Sprite)
    {
        super();

        _display = display;

        myMC.addEventListener(MouseEvent.MOUSE_DOWN, onPickUp, false, 0, true ); 

        display.stage.addEventListener(MouseEvent.MOUSE_UP, onDrop, false, 0, true ); 
        display.stage.addEventListener(Event.MOUSE_LEAVE, onMouseLeave, false, 0, true ); 
    }

    protected function onMouseLeave(event:Event):void
    {
        trace("Mouse Is Leaving The Stage");

    }

    protected function onDrop(e:MouseEvent):void
    {
        _display.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMoveObject);

    }   

    private function onPickUp(e:MouseEvent)
    {
        _dragedObject = e.currentTarget as MovieClip;

        _display.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMoveObject, false, 0, true);
    }

    protected function onMoveObject(e:MouseEvent):void
    {
        var point:Point = new Point(_display.stage.mouseX, _display.stage.mouseY);

            (_dragedObject as MovieClip).x = point.x;
            (_dragedObject as MovieClip).y = point.y;           
    }
}

这是一个例子: Simple Code

最简单的方法可能是使用 getBounds(stage) 并与 stageWidthstageHeight 进行比较:

var bounds:Rectangle = _draggedObject.getBounds(stage);
if (bounds.left < 0) {
    // left part of object is off-screen
} else if (bounds.right > stage.stageWidth) {
    // right part of object is off-screen
}
if (bounds.top < 0) {
    // top part of object is offscreen
} else if (bounds.bottom > stage.stageHeight) {
    // bottom part of object is off-screen
}

您可以在每种情况下移动 display

您可以尝试创建一个比您的舞台小一点的隐形区域。

因此您可以将 MOUSE_LEAVE 事件添加到该区域,当您的鼠标离开该区域时,您可以执行任何操作。

Check the example here.

针对 Aaron Beall 的回复:

为了更有趣的效果,如果你想等到影片剪辑完全离开舞台,你可以交换你在对象上检查的边界

var bounds:Rectangle = object.getBounds(stage);
if (bounds.right < 0) {
    // do thing
} else if (bounds.left > stage.stageWidth) {
    // do thing
}
if (bounds.bottom < 0) {
    // do thing
} else if (bounds.top > stage.stageHeight) {
    // do thing
}

如果在 class.

中,请确保已导入 import flash.geom.Rectangle;