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 に答える