使用边界矩形 AS3 防止 MC 离开舞台
Preventing MC from leaving stage with bounding rectangle AS3
我已经尝试了其他帖子中的许多建议,但无法完全发挥作用。
我想在我的舞台周围拖动一个对象,但我不希望该对象真正离开舞台。
使用下面的例子时,MC被舞台的顶部和底部束缚,但左侧仍然吞噬了被拖拽的MC的一半,而右侧不允许MC靠近边缘 - 它只有空白 space(看起来与 MC 在左侧被拖到外面的宽度相同)
var my_x:int=stage.stageWidth-box_mc.width;
var my_y:int=stage.stageHeight-box_mc.height;
var myWidth:int=0-my_x;
var myHeight:int=0-my_y;
var boundArea:Rectangle=new Rectangle(my_x, my_y, myWidth, myHeight);
box_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
box_mc.addEventListener(MouseEvent.MOUSE_UP, drop);
function drag(event:MouseEvent):void {
box_mc.startDrag(false,boundArea);
}
function drop(event:MouseEvent):void {
box_mc.stopDrag();
}
我相信代码是正确的,一定有我遗漏的东西 - 有人可以帮我试试这个并指出正确的方向吗?
非常感谢,
克里斯
看来您的注册点是您遇到的行为的罪魁祸首。
你应该使用类似的东西:
var boxArea:Rectangle = box_mc.getBounds(box_mc);
var boundArea:Rectangle = new Rectangle(-boxArea.x, -boxArea.y, stage.stageWidth-boxArea.width, stage.stageHeight-boxArea.height);
box_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
box_mc.addEventListener(MouseEvent.MOUSE_UP, drop);
function drag(event:MouseEvent):void {
box_mc.startDrag(false,boundArea);
}
function drop(event:MouseEvent):void {
box_mc.stopDrag();
}
基本上,在上面的代码中,您获取 box_mc 对象的矩形大小并将其存放在 boxArea 变量,并确保将其大小和注册点效果包含在 boundArea 计算中。
我已经尝试了其他帖子中的许多建议,但无法完全发挥作用。
我想在我的舞台周围拖动一个对象,但我不希望该对象真正离开舞台。
使用下面的例子时,MC被舞台的顶部和底部束缚,但左侧仍然吞噬了被拖拽的MC的一半,而右侧不允许MC靠近边缘 - 它只有空白 space(看起来与 MC 在左侧被拖到外面的宽度相同)
var my_x:int=stage.stageWidth-box_mc.width;
var my_y:int=stage.stageHeight-box_mc.height;
var myWidth:int=0-my_x;
var myHeight:int=0-my_y;
var boundArea:Rectangle=new Rectangle(my_x, my_y, myWidth, myHeight);
box_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
box_mc.addEventListener(MouseEvent.MOUSE_UP, drop);
function drag(event:MouseEvent):void {
box_mc.startDrag(false,boundArea);
}
function drop(event:MouseEvent):void {
box_mc.stopDrag();
}
我相信代码是正确的,一定有我遗漏的东西 - 有人可以帮我试试这个并指出正确的方向吗?
非常感谢,
克里斯
看来您的注册点是您遇到的行为的罪魁祸首。
你应该使用类似的东西:
var boxArea:Rectangle = box_mc.getBounds(box_mc);
var boundArea:Rectangle = new Rectangle(-boxArea.x, -boxArea.y, stage.stageWidth-boxArea.width, stage.stageHeight-boxArea.height);
box_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
box_mc.addEventListener(MouseEvent.MOUSE_UP, drop);
function drag(event:MouseEvent):void {
box_mc.startDrag(false,boundArea);
}
function drop(event:MouseEvent):void {
box_mc.stopDrag();
}
基本上,在上面的代码中,您获取 box_mc 对象的矩形大小并将其存放在 boxArea 变量,并确保将其大小和注册点效果包含在 boundArea 计算中。