Drag and Drop; Stage boundaries

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



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);