13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with geometry<br />

The inflatePt() method works similarly, except that it takes a Point object as its parameter, rather than dx and dy<br />

values.<br />

Finding unions and intersections of Rectangle objects<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

You use the union() method to find the rectangular region formed by the boundaries of two rectangles:<br />

import flash.display.*;<br />

import flash.geom.Rectangle;<br />

var rect1:Rectangle = new Rectangle(0, 0, 100, 100);<br />

trace(rect1); // (x=0, y=0, w=100, h=100)<br />

var rect2:Rectangle = new Rectangle(120, 60, 100, 100);<br />

trace(rect2); // (x=120, y=60, w=100, h=100)<br />

trace(rect1.union(rect2)); // (x=0, y=0, w=220, h=160)<br />

You use the intersection() method to find the rectangular region formed by the overlapping region of two<br />

rectangles:<br />

import flash.display.*;<br />

import flash.geom.Rectangle;<br />

var rect1:Rectangle = new Rectangle(0, 0, 100, 100);<br />

trace(rect1); // (x=0, y=0, w=100, h=100)<br />

var rect2:Rectangle = new Rectangle(80, 60, 100, 100);<br />

trace(rect2); // (x=120, y=60, w=100, h=100)<br />

trace(rect1.intersection(rect2)); // (x=80, y=60, w=20, h=40)<br />

You use the intersects() method to find out whether two rectangles intersect. You can also use the intersects()<br />

method to find out whether a display object is in a certain region of the Stage. For the following code example, assume<br />

the coordinate space of the display object container that contains the circle object is the same as that of the Stage.<br />

The example shows how to use the intersects() method to determine if a display object, circle, intersects specified<br />

regions of the Stage, defined by the target1 and target2 Rectangle objects:<br />

import flash.display.*;<br />

import flash.geom.Rectangle;<br />

var circle:Shape = new Shape();<br />

circle.graphics.lineStyle(2, 0xFF0000);<br />

circle.graphics.drawCircle(250, 250, 100);<br />

addChild(circle);<br />

var circleBounds:Rectangle = circle.getBounds(stage);<br />

var target1:Rectangle = new Rectangle(0, 0, 100, 100);<br />

trace(circleBounds.intersects(target1)); // false<br />

var target2:Rectangle = new Rectangle(0, 0, 300, 300);<br />

trace(circleBounds.intersects(target2)); // true<br />

Similarly, you can use the intersects() method to find out whether the bounding rectangles of two display objects<br />

overlap. Use the getRect() method of the DisplayObject class to include any additional space that the strokes of a<br />

display object add to a bounding region.<br />

Other uses of Rectangle objects<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

Rectangle objects are used in the following methods and properties:<br />

Last updated 6/6/2012<br />

214

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!