Доброго времени. Вообщем-то проблема такая. Есть объект, который перемещается по экрану. Нужно сделать так, чтобы он не вылазил за границы.

Код AS1/AS2:
stop ();
_global.movingLegs = new Array("l2", "l3", "l4", "l5", "r2", "r3", "r4");
_global.freeLegs = new Array(_root.crab_mc.l1, _root.crab_mc.l1.arm, _root.crab_mc.r1, _root.crab_mc.r1.arm);
var cSize = 80;
_root.createEmptyMovieClip("tracks_mc", 1);
crab_mc.swapDepths(tracks_mc);
var cTrackNum = 1;
crab_mc._x = cSize + MathPlus.rnd(Stage.width * 7.500000E-001);
crab_mc._y = cSize + MathPlus.rnd(Stage.height * 7.500000E-001);
crab_mc._rotation = MathPlus.rnd(180);
var duration;
var frameOffset = 0;
var crab_xTo;
var crab_yTo;
var crab_alpha;
var crab_isAnimated = false;
_root.onEnterFrame = function ()
{
if (frameOffset <= 0)
{
frameOffset = MathPlus.rnd(30, 200);
crab_xTo = cSize + MathPlus.rnd(Stage.width - cSize / 2);
crab_yTo = cSize + MathPlus.rnd(Stage.height - cSize / 2);
crab_alpha = 5.729578E+001 * Math.atan2(crab_yTo - crab_mc._y, crab_xTo - crab_mc._x);
if (crab_alpha > 90)
{
crab_alpha = -180 + crab_alpha;
}
else if (crab_alpha < -90)
{
crab_alpha = 180 + crab_alpha;
} // end else if
var _loc11 = Math.sqrt(Math.pow(crab_xTo - crab_mc._x, 2) + Math.pow(crab_yTo - crab_mc._y, 2));
if (_loc11 <= crab_mc._width)
{
duration = Math.round(Math.abs((crab_alpha - crab_mc._rotation) * 8.000000E-001));
}
else
{
duration = MathPlus.rnd3(4.000000E-001 * _loc11, 6.000000E-001 * _loc11);
} // end else if
crab_mc.slide({_x: crab_xTo, _y: crab_yTo, _rotation: crab_alpha}, "easeIn", duration);
frameOffset = frameOffset + duration;
}
else
{
--frameOffset;
} // end else if
if (duration > 0)
{
--duration;
for (iL in movingLegs)
{
if (movingLegs[iL].length == undefined)
{
continue;
} // end if
var _loc3 = crab_mc[movingLegs[iL]];
if (_loc3.onEnterFrame == undefined)
{
if (_loc3.dir == "down" || _loc3.dir == undefined)
{
_loc3.dir = "up";
var _loc4 = {x: 0, y: 0};
_loc3.c.localToGlobal(_loc4);
_root.tracks_mc.attachMovie("track_anim", "track_" + cTrackNum, cTrackNum, {_x: _loc4.x, _y: _loc4.y, _alpha: MathPlus.rnd(20, 75)});
if (++cTrackNum > 1000)
{
cTrackNum = 1;
} // end if
_loc3.pauseExec(16 * MathPlus.rnd(1, 3), function ()
{
this.slide({_rotation: MathPlus.rnd(15, 25)}, "easeOut", MathPlus.rnd(5, 6));
});
continue;
} // end if
_loc3.dir = "down";
_loc3.pauseExec(16 * MathPlus.rnd(1, 3), function ()
{
this.slide({_rotation: MathPlus.rnd(-1, 1)}, "easeOut", MathPlus.rnd(5, 6));
});
} // end if
} // end of for...in
}
else
{
for (iL in freeLegs)
{
_loc3 = freeLegs[iL];
trace (_loc3._name);
if (_loc3.onEnterFrame == undefined)
{
_loc3.pauseExec(16 * MathPlus.rnd(1, 3), function ()
{
this.slide({_rotation: MathPlus.rnd(-10, 10)}, "easeOut", MathPlus.rnd(5, 10));
});
} // end if
} // end of for...in
} // end else if
}