# Drag and Drop; Stage boundaries

**URL:** <https://forum.kirupa.com/t/drag-and-drop-stage-boundaries/321426>\
**Category:** flash\
**Created:** [April 19, 2011, 4:43pm UTC](https://forum.kirupa.com/t/drag-and-drop-stage-boundaries/321426 "2011-04-19T16:43:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![keitho30](https://avatars.discourse-cdn.com/v4/letter/k/b5a626/32.png) [@keitho30](https://forum.kirupa.com/u/keitho30)\
**Post date:** [April 19, 2011, 4:43pm UTC](https://forum.kirupa.com/t/drag-and-drop-stage-boundaries/321426/1 "2011-04-19T16:43:08Z")

</div>

Hi guys,

i have a drag and drop function working and i need to prevent them from being dragged off stage. I have some code but cannot seem to figure it out… I have followed an iliketoflash tutorial. Thanks in advance

```auto

function limitStageBorder(e:MouseEvent):void {
   
   var targetHalfWidth:uint=e.target.width/2;
   var targetHalfHeight:uint=e.target.height/2;
 
   if (e.currentTarget.x+targetHalfWidth>stage.stageWidth) {
       e.currentTarget.x=stage.stageWidth-targetHalfWidth;
   }
   else if (e.currentTarget.x - targetHalfWidth <0) {        
       e.currentTarget.x=0+targetHalfWidth;    
   }     
   
   if (e.currentTarget.y-targetHalfHeight<0) {        
       e.currentTarget.y=0+targetHalfHeight;    
   }    
   else if (e.currentTarget.y + targetHalfHeight > stage.stageHeight) {
       e.currentTarget.y=stage.stageHeight-targetHalfHeight;
   }
}

limitStageBorder(e.currentTarget);
		

```
