title: AS3 How can I constrain an objects movement within the stage?
body: <p>My object is controlled via mouse movement (for now)... Because of the way it's controlled (object always moves away from cursor), I want to add constraints so that it not only stays within the stage but leaves space between the movement boundaries and the stage edges... </p>
  
  <p>Therefore if the object moves too close to the stage limits, there will be room for the cursor to move it back into the play area.</p>
  
  <p>Currently, I have a rectangle created dynamically within the stage and I was thinking I could constrain movement to within the area of this rectangle, which would leave enough room around the edges... How can I do this?</p>
  
  <p>However, if there's a better/easier way to get the desired results... I'm all ears.</p>
  
  <pre><code>import flash.events.KeyboardEvent;
  import flash.display.MovieClip;
  import flash.events.Event;
  import flash.display.Shape;
  
  var rectangle:Shape = new Shape;
  //initialize "rectangle" shape
  rectangle.graphics.beginFill(0xCCCCCC); 
  //choose colour for fill - black
  rectangle.graphics.drawRect(50, 50, 450, 300);
  //draw shape
  rectangle.graphics.endFill(); 
  //end fill
  addChild(rectangle);
  //add "rectangle" to stage
  
  var hero:MovieClip = new hero_mc();
  //initialize "hero" object - "hero_mc"
  
  hero.x = stage.stageWidth / 2;
  hero.y = stage.stageHeight / 2;
  //set spawn location, centre stage
  
  addChild(hero);
  //add "hero" to stage
  
  function cursorHold(evt:Event):void {
      trace("moving");
  
      var dX:Number = hero.x - stage.mouseX;
      //get adjacent
      var dY:Number = hero.y - stage.mouseY;
      //get opposite
  
      var r:Number = Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2));
      //get hypotenuse
  
      var angle:Number = Math.acos(dX/r)*180/Math.PI;
      //get angle
  
      var radians:Number = deg2rad(angle);
      //call conversion function for angle
  
      var speed:Number = 1.5;
      //set speed
  
      var xV:Number = Math.cos(radians) * speed;
      //get x velocity
      var yV:Number = Math.sin(radians) * speed;
      //get y velocity
  
      hero.x += xV;
      //move hero along new x velocity
      if (stage.mouseY &gt; hero.y) {
          hero.y -= yV;
      } else {
          hero.y += yV;
      } 
      //move hero along new y velocity    
  }
  
  function deg2rad(deg:Number):Number {
      return deg * (Math.PI / 180);
      //convert degrees to radians
  }
  
  stage.addEventListener(Event.ENTER_FRAME, cursorHold, false, 0, true);
  </code></pre>
  
  <p>Here's an image: <a href="https://lh5.googleusercontent.com/lWG8uR9MLK32oHXX26JBiLtBdvmiICFuxOQakQESnBY=w552-h402-no" rel="nofollow">https://lh5.googleusercontent.com/lWG8uR9MLK32oHXX26JBiLtBdvmiICFuxOQakQESnBY=w552-h402-no</a></p>
  
  <p>So, "hero_mc" shouldn't be able to move into the white area but it should still move (scrape against the edges with the mouse movement.</p>
  
  <p>Apologies if the code is a mess... I'm pretty new to ActionScript 3.0. Any tips on cleaning it up a bit would also be more than welcome.</p>