我有一个应用程序,用户上传图像,然后通过单击,拖动和使用调整大小栏来转换图像。但是我的客户要求我限制用户可以拖动到的位置,这不是问题,除了我需要限制的方式与正常情况相反。
所以我的startDrag(假,新的Rectangle ......)工作得很好,但我需要的是让用户能够在边界之外拖动而不会在flash的内部有空白区域文件。
我的意思是说我有一个500px宽的闪存文件和里面的图像,我不小心拖了一下。如果图像右手边缘(如果我向左拖动)达到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();
}
答案 0 :(得分: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);