Limitador rectangular sin condicionales

Un método super sencillo para aplicar un límite rectangular a un elemento arrastrable (si no te va bien utilizar startDrag):

Actionscript:
  1. arrastrable.x = Math.min( Math.max(limite.x,arrastrable.x), limite.width-arrastrable.width );
  2. arrastrable.y = Math.min( Math.max(limite.y,arrastrable.y), limite.height-arrastrable.height );

Un ejemplo de implementación en AS2:

Actionscript:
  1. import flash.geom.Rectangle;
  2. import mx.utils.Delegate;
  3.  
  4. var limite:Rectangle = new Rectangle (100, 100, 200, 100);
  5. arrastrable.onPress = Delegate.create (this, start_drag);
  6. arrastrable.onRelease = arrastrable.onReleaseOutside = Delegate.create (this, stop_drag);
  7. function start_drag ()
  8. {
  9.     arrastrable.onEnterFrame = Delegate.create (this, doDrag);
  10. }
  11. function stop_drag ()
  12. {
  13.     arrastrable.onEnterFrame = null;
  14. }
  15. function doDrag ()
  16. {
  17.     arrastrable._x = _xmouse;
  18.     arrastrable._y = _ymouse;
  19.     arrastrable._x = Math.min (Math.max (limite.x, arrastrable._x), (limite.x + limite.width) - arrastrable._width);
  20.     arrastrable._y = Math.min (Math.max (limite.y, arrastrable._y), (limite.y + limite.height) - arrastrable._height);
  21. }