检查影片剪辑的一部分何时离开舞台



我正在使用 AS3 创建一个拖放游戏,我想检查何时将影片剪辑的一部分移到屏幕之外,以将视图移动到后面并让用户选择放置它的位置。

我无法测试影片剪辑凭据是否大于舞台 (scaleMode = NO_SCALE) 宽度/高度,因为舞台的一部分隐藏在浏览器窗口后面。

这与MOUSE_LEAVE相同的方面,只是这次必须用于MovieClips,我试图查看MOUSE_LEAVE背后的代码,但我无法访问它。

谢谢。

主班

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

世界级

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

下面是一个例子:简单的代码

最简单的方法可能是使用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事件添加到该区域,当鼠标离开该区域时,您可以执行所需的操作。

在此处查看示例。

回应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
}

如果这在类中,请确保已导入import flash.geom.Rectangle;

最新更新