1

想不出来这个。如何将注册点在中心的 mc 的拖动约束到注册点也在中心的较大剪辑?下面的代码拖动名为 img 的剪辑,但不限制为“rect”剪辑。

var bounds:Rectangle = new Rectangle(rect.width/2-img.width/2, rect.height/2-img.height/2, rect.width-img.width, rect.height-img.height);

img.addEventListener(MouseEvent.MOUSE_DOWN, drag);
img.addEventListener(MouseEvent.MOUSE_UP, drop);

function drag(event:MouseEvent) {
    img.startDrag(false, bounds);
}
function drop(event:MouseEvent) {
    img.stopDrag();
}
4

2 回答 2

1

如果rect是您希望对象 ( img) 留在其中的矩形,并且它们都锚定在中心,那么这就是您需要的:

var bounds:Rectangle = new Rectangle();
bounds.x = rect.x + (img.width * .5) - (rect.width * .5);  //offset by + half of the image, and - half of the rectangle
bounds.y = rect.y + (img.height * .5) - (rect.height * .5);
bounds.width = rect.width - img.width; //make the bounds the size of the rectangle, minus the size of the image
bounds.height = rect.height - img.height;
于 2012-09-13T21:30:29.793 回答
0

注册点与拖动边界框无关。

例如

new Rectangle(0,0,100,100)

仅允许 MovieCLip 仅从舞台左上角的 0,0 移动到 100,100。

[编辑]

boundingBox.graphics.moveTo(0,0);
boundingBox.graphics.beginFill(0);
boundingBox.graphics.lineTo(400,0);
boundingBox.graphics.lineTo(400,400);
boundingBox.graphics.lineTo(0,400);
boundingBox.graphics.lineTo(0,0);
boundingBox.graphics.endFill();
boundingBox.x = 100;
boundingBox.y = 100;


dragTarget.graphics.moveTo(0,0);
dragTarget.graphics.beginFill(0xff0000);
dragTarget.graphics.drawRect(0,0,10,10);
dragTarget.graphics.endFill();
dragTarget.x = 150;
dragTarget.y = 150;

container.addChild(boundingBox) 
container.addChild(dragTarget) 
this.addChild(container)

dragTarget.addEventListener(MouseEvent.MOUSE_DOWN,onDown)


var boundingBox:Sprite = new Sprite();
var dragTarget:Sprite = new Sprite();
var container:Sprite = new Sprite()
function onDown(event:Event):void{
  var bounds:Rectangle = new Rectangle(boundingBox.x,
                 boundingBox.y,
                 boundingBox.width-dragTarget.width,
                 boundingBox.height-dragTarget.height)
  dragTarget.startDrag( false,bounds )
}
于 2012-09-13T21:18:26.527 回答