User Guide
420 Working with Geometry
Using Rectangle objects
A Rectangle object defines a rectangular area. A Rectangle object has a position, defined by
the
x and y coordinates of its top-left corner, a width property, and a height property. You
can define these properties for a new Rectangle object by invoking the
Rectangle()
constructor function, as follows:
import flash.geom.Rectangle;
var rx:Number = 0;
var ry:Number = 0;
var rwidth:Number = 100;
var rheight:Number = 50;
var rect1:Rectangle = new Rectangle(rx, ry, rwidth, rheight);
Resizing and repositioning Rectangle objects
There are a number of ways to resize and reposition Rectangle objects.
You can directly reposition the Rectangle object by changing its
x and y properties. This has
no effect on the width or height of the Rectangle object.
import flash.geom.Rectangle;
var x1:Number = 0;
var y1:Number = 0;
var width1:Number = 100;
var height1:Number = 50;
var rect1:Rectangle = new Rectangle(x1, y1, width1, height1);
trace(rect1) // (x=0, y=0, w=100, h=50)
rect1.x = 20;
rect1.y = 30;
trace(rect1); // (x=20, y=30, w=100, h=50)
As the following code shows, if you change the left or top property of a Rectangle object, it
is also repositioned, with its
x and y properties matching the left and top properties,
respectively. However, the position of the bottom-left corner of the Rectangle object does not
change, so it is resized.
import flash.geom.Rectangle;
var x1:Number = 0;
var y1:Number = 0;
var width1:Number = 100;
var height1:Number = 50;
var rect1:Rectangle = new Rectangle(x1, y1, width1, height1);
trace(rect1) // (x=0, y=0, w=100, h=50)
rect1.left = 20;
rect1.top = 30;
trace(rect1); // (x=30, y=20, w=70, h=30)