Urgent help needed: Restricting object(movie Clip) in a defined boundry

Hi,

I am new to flash. I am trying to create a game right now. Soewhat same at the game “This is the only level”(http://armorgames.com/play/4309/this-is-the-only-level)

I want to restrict the cartoon(movie object) within this boundary which I have created using line tool(I can even create rectangles if needed).

Here is the code so far

var mycartoon:cartoon = new cartoon();
mycartoon.x=stage.stageWidth/2;
mycartoon.y=stage.stageHeight/2;
addChild(mycartoon);
mycartoon.name="mycartoon";
mycartoon.x = 200;
mycartoon.y = 100;





mycartoon.addEventListener(MouseEvent.MOUSE_OUT, rotatecartoon);
function rotatecartoon(event:Event):void {
	
	
	var rad:Number=Math.atan2(mouseY-mycartoon.y,mouseX-mycartoon.x);
	

	var p1:Point=new Point(mouseX,mouseY);
	var p2:Point=new Point(mycartoon.x,mycartoon.y);
	var distance:Number=Point.distance(p1,p2);

	var polar:Point=Point.polar(distance,rad);
	mycartoon.x+=polar.x;
	mycartoon.y+=polar.y;

}

Any advice will be really appreciated. Thanks in advance

edit: I cant use startdrag as I will be using keyboard controls as well to move the cartoon