Moving a display object by a specified angle and distance
You can use the polar() method of the Point class to move a display object a specific distance by a specific angle. For example, the following code moves the myDisplayObject object 100 pixels by 60 degrees: import flash.geom.*; var distance:Number = 100; var angle:Number = 2 * Math.PI * (90 / 360); var translatePoint:Point = Point.polar(distance, angle); myDisplayObject.x += translatePoint.x; myDisplayObject.y += translatePoint.y;
Other uses of the Point class
You can use Point objects with the following methods and properties:
Class
Rectangle
Methods or properties deltaTransformPoint() transformPoint() bottomRight size topLeft
Description
DisplayObjectContainer areInaccessibleObjectsUnderPoint() getObjectsUnderPoint()
Used to return a list of objects under a point in a display object container.
BitmapData
BitmapData hitTest() applyFilter() copyChannel() merge() paletteMap() pixelDissolve() threshold()
Used to define the pixel in the BitmapData object as well as the point that you are checking for a hit.
Used to define the positions of rectangles that define the operations.
Matrix Used to define points for which you want to apply a transformation.
Used to define these properties.
Using Point objects 419
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)
420 Working with Geometry
Similarly, as the following example shows, if you change the bottom or right property of a
Rectangle object, the position of its top-left corner does not change, so it is resized accordingly: 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.right = 60; trect1.bottom = 20; trace(rect1); // (x=0, y=0, w=60, h=20)
You can also reposition a Rectangle object by using the offset() method, as follows: 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.offset(20, 30); trace(rect1); // (x=20, y=30, w=100, h=50)
The offsetPt() method works similarly, except that it takes a Point object as its parameter, rather than x and y offset values.
You can also resize a Rectangle object by using the inflate() method, which includes two parameters, dx and dy . The dx parameter represents the number of pixels that the left and right sides of the rectangle will move from the center, and the dy parameter represents the number of pixels that the top and bottom of the rectangle will move from the center: 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.inflate(6,4); trace(rect1); // (x=-6, y=-4, w=112, h=58)
The inflatePt() method works similarly, except that it takes a Point object as its parameter, rather than dx and dy values.
Using Rectangle objects 421