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

时间:2016-02-08 10:30:21

标签: actionscript-3 flash mouseevent movieclip stage

我正在使用AS3创建一个拖放游戏,我想检查一下Movieclip的一部分是否在屏幕之外,以便移动View并让用户选择放下它的位置。

我不能'测试MovieClip凭据是否大于舞台(scaleMode = NO_SCALE)Width / Height,因为舞台的一部分隐藏在浏览器窗口后面。

这与MOUSE_LEAVE的方面相同,这次它必须是MovieClip,我试图看到MOUSE_LEAVE背后的代码,但我无法达到它。

谢谢。

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

WORLD 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

3 个答案:

答案 0 :(得分:1)

最简单的方法可能是使用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

答案 1 :(得分:0)

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

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

Check the example here

答案 2 :(得分:0)

回应亚伦·比尔的回应:

要获得更有趣的效果,如果要等到影片剪辑完全脱离舞台,可以交换在对象上检查的边界

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;