2011-09-12 67 views
0

我有一個應用程序,用戶上傳圖像,然後通過單擊,拖動和使用調整大小欄來轉換它。但是我的客戶要求我限制用戶可以拖到哪裏,這不是問題,除非我需要與正常情況相反的限制。ActionScript拖動限制,向後?

所以,我在哪裏startDrag(假,新的矩形...)工作得很好,但我需要的是讓用戶能夠拖動邊界之外,並沒有在內部的空白空間閃存文件。

我的意思是說我有一個500像素寬的閃存文件和一個裏面的圖像,我不小心拖着。如果圖像的右手邊緣(如果我向左拖動)命中500px,它將停止拖動,並且不允許它們向左拉。

我真的很希望我解釋得很好,任何指導都會很棒!

下面是我目前對拖動事件的代碼。

任何幫助將非常感激。

public function startImageDrag (e:MouseEvent):void { 
     mousePos['x'] = e.target.mouseX; 
     mousePos['y'] = e.target.mouseY; 
     imageDraggable.removeEventListener(MouseEvent.MOUSE_DOWN, function():void {}); 
     photoapp.cStage.addEventListener(MouseEvent.MOUSE_MOVE, moveImage); 
     photoapp.cStage.addEventListener(MouseEvent.MOUSE_UP, endImageDrag); 
    } 
    //The type is wildcarded because I have this hooked to MOUSE_LEAVE too 
    public function endImageDrag (e:*):void { 
     photoapp.cStage.removeEventListener(MouseEvent.MOUSE_MOVE, moveImage); 
     photoapp.cStage.addEventListener(MouseEvent.MOUSE_DOWN, startImageDrag); 
    } 

    public function moveImage(event:MouseEvent):void { 
     //Get the offset of the current mouse position over the image 
     var 
      //The mouse position on the stage 
      sxOff:Number = photoapp.cStage.mouseX, 
      syOff:Number = photoapp.cStage.mouseY, 
      //The position on which the mouse down event was on the image 
      reX:Number = sxOff - mousePos['x'], 
      reY:Number = syOff - mousePos['y'], 
      //The iamge object 
      i:DisplayObject = imageDraggable; 

     //Move the image 
     if (/*I have no idea now...*/) { 
      imageDraggable.x = reX; 
     } 
     if (iY) { 
      imageDraggable.y = reY; 
     } 

     event.updateAfterEvent(); 
    } 

回答

1
// this code will limit the movement to 100 pixels to the right 
var bounds:Rectangle = new Rectangle(0,0,100,0) 
imageDraggable.startDrag(false,bounds); 

[編輯]
也許這將幫助你更多的瞭解

var boundsWidth:Number = stage.stageWidth - pictureHolderMC.width); 
var boundsHeight:Number = stage.stageHeight - pictureHolderMC.height); 
var bounds:Rectangle = new Rectangle(0, 0, boundsWidth, boundsHeight); 
pictureHolderMC.startDrag(false, bounds); 
+0

嗨,我知道如何做到這一點..這個問題問到如何拖動目標限制在舞臺的邊緣。 I.E它們不能被拖拽到** 0,0,stage.stageWidth,stage.stageHeight **之外,以便它始終有內容而不是空白。 –

+0

Rectangle(0,0,stage.stageWidth-imageDraggable.width,stage.stageHeight-imageDraggable.height) –

+0

對不起,更好的解釋方法是左上角只能是負數(舞臺外)底部和右側只允許在階段尺寸之外。對不起剛看到你的答案,我會試試。 –