The Rectangle Class


The Rectangle Class

The Rectangle class allows you to create rectangles at runtime.

It is important when using this class to import it first at the beginning of your script, like this:

 import flash.geom.Rectangle; 

To instantiate a new instance of the Rectangle class, use this code as a template:

 var myRectangle:Rectangle = new Rectangle(x, y, width, height); 

  • x The x coordinate at the top-left corner.

  • y The y coordinate at the top-left corner.

  • width The width of the rectangle.

  • height The height of the rectangle.

Example:

This example will create an instance of the Rectangle class and send its information to the Output panel:

 import flash.geom.Rectangle; var myRectangle:Rectangle = new Rectangle(10,10,100,100); trace(myRectangle); //output: (x=10, y=10, w=100, h=100) 

There are a few more properties, which were not in the parameters.

Properties

bottom

Availability: FP:8, AS:1.0

Generic Template: myRectangle.bottom;

Description:

This numeric property is the sum of the y property and the height property.

bottomRight

Availability: FP:8, AS:1.0

Generic Template: myRectangle.bottomRight;

Description:

This property is a Point object with the (x,y) coordinate being the bottom-right corner of the rectangle.

left

Availability: FP:8, AS:1.0

Generic Template: myRectangle.left;

Description:

This numeric property is the x property representing the left side of the rectangle.

right

Availability: FP:8, AS:1.0

Generic Template: myRectangle.right;

Description:

This numeric property is the sum of the x property and width property representing the right side of the rectangle.

size

Availability: FP:8, AS:1.0

Generic Template: myRectangle.size;

Description:

This property is a Point object, but the values (x,y) stand for (width, height).

top

Availability: FP:8, AS:1.0

Generic Template: myRectangle.top;

Description:

This numeric property is the y property representing the top side of the rectangle.

topLeft

Availability: FP:8, AS:1.0

Generic Template: myRectangle.topLeft;

Description:

This property is expressed as a Point object containing the coordinates for the top left corner of the rectangle.

Example:

This example will create a rectangle and trace every property it has:

 import flash.geom.Rectangle; var myRectangle:Rectangle = new Rectangle(10,10,100,100); trace("x - "+myRectangle.x); trace("y - "+myRectangle.y); trace("width - "+myRectangle.width); trace("height - "+myRectangle.height); trace("left - "+myRectangle.left); trace("topLeft - "+myRectangle.topLeft); trace("top - "+myRectangle.top); trace("right - "+myRectangle.right); trace("bottomRight - "+myRectangle.bottomRight); trace("bottom - "+myRectangle.bottom); trace("size - "+myRectangle.size); //output: x - 10 //y - 10 //width - 100 //height - 100 //left - 10 //topLeft - (x=10, y=10) //top - 10 //right - 110 //bottomRight - (x=110, y=110) //bottom - 110 //size - (x=100, y=100) 

Methods

clone

Availability: FP:8, AS:1.0

Generic Template: myRectangle.clone();

Returns: Rectangle

This method will return an exact duplicate of the Rectangle object it is being called on.

Description:

This method will create an exact copy of the Rectangle object it is being called on.

Example:

This example will create a Rectangle object, create a copy, and output that copy:

 import flash.geom.Rectangle; var myRectangle:Rectangle = new Rectangle(10,10,100,100); var myRec2:Rectangle = myRectangle.clone(); trace(myRec2); //output: (x=10, y=10, w=100, h=100) 

contains

Availability: FP:8, AS:1.0

Generic Template: myRectangle.contains(x, y);

Parameters:

  • x The horizontal position of the point being checked.

  • y The vertical position of the point being checked.

Returns: Boolean

If the coordinate falls within the rectangle, true will be returned. Otherwise, false will be returned.

Description:

This method will check to see if a coordinate (x,y) falls within a rectangle.

Example:

This example will check to see if two coordinates are within a rectangle:

 import flash.geom.Rectangle; var myRectangle:Rectangle = new Rectangle(10,10,100,100); trace(myRectangle.contains(5,5)); trace(myRectangle.contains(20,20)); //output: false //        true 

containsPoint

Availability: FP:8, AS:1.0

Generic Template: myRectangle.containsPoint(point);

Parameters:

  • point A Point object containing the (x,y) coordinate to check.

Returns: Boolean

If the Point falls within the rectangle, true will be returned. Otherwise, false will be returned.

Description:

This method will check to see if a Point falls within a rectangle.

Example:

This example will check to see if two Points are within a rectangle:

 import flash.geom.Rectangle; import flash.geom.Point; //the points var point1:Point = new Point(5,5); var point2:Point = new Point(20,20); var myRectangle:Rectangle = new Rectangle(10,10,100,100); trace(myRectangle.containsPoint(point1)); trace(myRectangle.containsPoint(point2)); //output: false //        true 

containsRectangle

Availability: FP:8, AS:1.0

Generic Template: myRectangle.containsRectangle(rec);

Parameters:

  • rec A Rectangle object.

Returns: Boolean

If the Rectangle falls entirely within the first rectangle, true will be returned. Otherwise, false will be returned.

Description:

This method will check to see if a Rectangle falls entirely within the first rectangle.

Example:

This example will check to see if a couple of Rectangles fall entirely within the first rectangle:

 import flash.geom.Rectangle; var myRectangle:Rectangle = new Rectangle(10,10,100,100); var myRec2:Rectangle = new Rectangle (20,20,20,20); var myRec3:Rectangle = new Rectangle (0,20,20,20); trace(myRectangle.containsRectangle(myRec2)); trace(myRectangle.containsRectangle(myRec3)); //output: true //        false 

equals

Availability: FP:8, AS:1.0

Generic Template: myRectangle.equals(rec);

Parameters:

  • rec A Rectangle object to compare to myRectangle.

Returns: Boolean

If rec is equal to myRectangle in the properties x, y, width and height, true will be returned. Otherwise, false will be returned.

Description:

This method will check the four main properties of two rectangles to see if they are equal.

Example:

This example will check to see if a couple of Rectangles are equal to one another:

 import flash.geom.Rectangle; var myRectangle:Rectangle = new Rectangle(10,10,100,100); var myRec2:Rectangle = new Rectangle (20,20,20,20); var myRec3:Rectangle = myRectangle.clone(); trace(myRectangle.equals(myRec2)); trace(myRectangle.equals(myRec3)); //output: false //        true 

inflatePoint

Availability: FP:8, AS:1.0

Generic Template: myRectangle.inflatePoint(point);

Parameters:

  • point The Point object used to inflate the rectangle.

Description:

This method will increase the size of the rectangle as well as adjust the x and y properties using a Point object.

Example:

This example will create a rectangle and then increase its dimensions:

 import flash.geom.Rectangle; import flash.geom.Point; //create the point var point:Point = new Point(5,5); var myRectangle:Rectangle = new Rectangle(10,10,100,100); trace(myRectangle); myRectangle.inflatePoint(point); trace(myRectangle); //output: (x=10, y=10, w=100, h=100) //(x=5, y=5, w=110, h=110) 

intersection

Availability: FP:8, AS:1.0

Generic Template: myRectangle.intersection(rec);

Parameters:

  • rec The Rectangle object being used to see if it intersects myRectangle.

Returns: Rectangle

The area of intersection is returned as a Rectangle.

Description:

This method will check to see if two Rectangles intersect, and if they do, a Rectangle object containing the intersection data is returned.

Example:

This example will create two Rectangles and see if they intersect:

 import flash.geom.Rectangle; var myRectangle:Rectangle = new Rectangle(10,10,100,100); var myRec2:Rectangle = new Rectangle(20,0,50,30); trace(myRectangle.intersection(myRec2)); //output: (x=20, y=10, w=50, h=20) 

intersects

Availability: FP:8, AS:1.0

Generic Template: myRectangle.intersects(rec);

Parameters:

  • rec The Rectangle object being used to see if it intersects myRectangle.

Returns: Boolean

true if the rectangles intersect; false if they do not.

Description:

This method will check to see if two Rectangles intersect and returns a Boolean response.

Example:

This example will create two Rectangles and see if they intersect:

 import flash.geom.Rectangle; var myRectangle:Rectangle = new Rectangle(10,10,100,100); var myRec2:Rectangle = new Rectangle(20,0,50,30); trace(myRectangle.intersects(myRec2)); //output: true 

isEmpty

Availability: FP:8, AS:1.0

Generic Template: myRectangle.isEmpty();

Returns: Boolean

If the rectangle has neither width nor height, true is returned. Otherwise, false is returned.

Description:

This method will check to see if a rectangle is empty (absent width and height).

Example:

This example will check to see if either of the two Rectangles are empty:

 import flash.geom.Rectangle; var myRectangle:Rectangle = new Rectangle(10,10,100,100); var myRec2:Rectangle = new Rectangle(); trace(myRectangle.isEmpty()); trace(myRec2.isEmpty()); //output: false //        true 

offset

Availability: FP:8, AS:1.0

Generic Template: myRectangle.offset(dx,dy);

Parameters:

  • dx The horizontal adjustment in pixels.

  • dy The vertical adjustment in pixels.

Description:

This method will adjust the x and y positions of the rectangle.

Example:

This example will create a Rectangle, output the information, adjust the position, and output the information again:

 import flash.geom.Rectangle; var myRectangle:Rectangle = new Rectangle(10,10,100,100); trace(myRectangle); myRectangle.offset(5,5); trace(myRectangle); //output: (x=10, y=10, w=100, h=100) //(x=15, y=15, w=100, h=100) 

offsetPoint

Availability: FP:8, AS:1.0

Generic Template: myRectangle.offsetPoint(point);

Parameters:

  • point The Point object that contains the horizontal and vertical adjustments being applied to the rectangle.

Description:

This method will adjust the x and y positions of the rectangle using a Point object.

Example:

This example will create a Rectangle, output the information, adjust the position, and output the information again:

 import flash.geom.Rectangle; import flash.geom.Point; //create the point var point:Point = new Point(5,5); var myRectangle:Rectangle = new Rectangle(10,10,100,100); trace(myRectangle); myRectangle.offsetPoint(point); trace(myRectangle); //output: (x=10, y=10, w=100, h=100) //(x=15, y=15, w=100, h=100) 

setEmpty

Availability: FP:8, AS:1.0

Generic Template: myRectangle.setEmpty();

Description:

This method will set all Rectangle options to 0.

Example:

This example will create a Rectangle, output the information, set it to empty, and output the information again:

 import flash.geom.Rectangle; var myRectangle:Rectangle = new Rectangle(10,10,100,100); trace(myRectangle); myRectangle.setEmpty(); trace(myRectangle); //output: (x=10, y=10, w=100, h=100) //(x=0, y=0, w=0, h=0) 

toString

Availability: FP:8, AS:1.0

Generic Template: myRectangle.toString();

Results: String

The Rectangles information.

Description:

This method will return the Rectangles information in the form of a string.

Example:

This example will create a Rectangle and output the information:

 import flash.geom.Rectangle; var myRectangle:Rectangle = new Rectangle(10,10,100,100); trace(myRectangle); //output: (x=10, y=10, w=100, h=100) 

union

Availability: FP:8, AS:1.0

Generic Template: myRectangle.union(rec);

Parameters:

  • rec The Rectangle object being joined.

Results: Rectangle

The combined dimensions of both Rectangles.

Description:

This method will combine both Rectangles at their absolute highest points.

Example:

This example will create two Rectangles and combine them to form one big Rectangle:

 import flash.geom.Rectangle; var myRectangle:Rectangle = new Rectangle(10,10,100,100); var myRec2:Rectangle = new Rectangle(50,50,80,80); trace(myRectangle.union(myRec2)); //output: (x=10, y=10, w=120, h=120) 




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net