我正在使用AS3创建一个拖放游戏,我想检查一下什么时候一个分离的Movieclip在屏幕外移动视图后面,让用户选择放置它的地方。
我不能测试MovieClip凭据是否比舞台(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;
}
}下面是一个例子:简单码
发布于 2016-02-08 19:57:29
最简单的方法可能是使用getBounds(stage)并与stageWidth和stageHeight进行比较。
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。
发布于 2016-02-09 10:59:59
你可以尝试创建一个看不见的区域,它比你的舞台要小一些。
因此,您可以将MOUSE_LEAVE事件添加到区域中,当鼠标离开该区域时,您可以做您想做的事情。
检查这里的示例。
发布于 2020-01-22 10:13:05
针对亚伦·比尔的答复:
为了获得更有趣的效果,如果您想等到电影剪辑完全离开舞台,您可以交换检查对象的边界。
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;在类中,请确保导入了它。
https://stackoverflow.com/questions/35267415
复制相似问题