首页 文章

如何防止在Flex / Flash中拖出父容器外?

提问于
浏览
1

我有一个画布,我在其上绘制像矩形等的形状 . 当我使用自定义鼠标按下/鼠标移动处理程序移动形状时,形状可以超出父边界 .

我正在检查孩子是否在父母的范围之外将其扣回:

var bounds:Rectangle = this.getBounds(this.parent);
   var p:Point;

   if (bounds.x <0) {
    p = new Point(0,0);
    p.offset(-bounds.x,0);
   }
   if (bounds.y <0) {
    if (!p) p = new Point(0,0);
    p.offset(0,-bounds.y);
   } 
   if (bounds.y+bounds.height > this.parent.height) {
    if (!p) p = new Point(0,0);
    p.offset(0,-bounds.y-height+this.parent.height);
   }
   if (bounds.x+bounds.width > this.parent.width) {
    if (!p) p = new Point(0,0);
    p.offset(-bounds.x-width+this.parent.width,0);
   }   
   if (p) {
    trace("invoking snapInside ", p);
           snapInside(p);

当我走出左边界或顶边界时,会调用snapInside方法 . 但是当孩子被拖到右边界或右边界外时,我发现Flex / Flash运行时会自动扩展父亲的身高和宽度 . 所以说父母大小最初是800x600,如果孩子的Y界限超过800,比方说20像素,我发现this.parent.height会自动调整大小到820!

当孩子超出父母的原始范围时,如何防止父母调整大小?

1 回答

  • 1

    创建父对象时,将边界保存到函数外部的var中,以便稍后调用存储的大小 . 如果你继续检查函数中的边界,那么它当然会随着父对象的变化而改变 . 或者在mouseDown上设置边界但不在拖动上设置边界,这样在拖动对象到达边缘之前就有了更新的边界 .

相关问题