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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Working with geometry<br />

var matrix:Matrix = new Matrix();<br />

var rotation:Number = 2 * Math.PI * (30 / 360); // 30°<br />

var scaleFactor:Number = 2;<br />

matrix.scale(scaleFactor, scaleFactor);<br />

matrix.rotate(rotation);<br />

matrix.scale(scaleX, scaleY);<br />

matrix.rotate(rotation);<br />

myDisplayObject.transform.matrix = matrix;<br />

To apply a skew transformation to a Matrix object, adjust its b or c property. Adjusting the b property skews the matrix<br />

vertically, and adjusting the c property skews the matrix horizontally. The following code skews the myMatrix Matrix<br />

object vertically by a factor of 2:<br />

var skewMatrix:Matrix = new Matrix();<br />

skewMatrix.b = Math.tan(2);<br />

myMatrix.concat(skewMatrix);<br />

You can apply a Matrix transformation to the transform property of a display object. For example, the following code<br />

applies a matrix transformation to a display object named myDisplayObject:<br />

var matrix:Matrix = myDisplayObject.transform.matrix;<br />

var scaleFactor:Number = 2;<br />

var rotation:Number = 2 * Math.PI * (60 / 360); // 60°<br />

matrix.scale(scaleFactor, scaleFactor);<br />

matrix.rotate(rotation);<br />

myDisplayObject.transform.matrix = matrix;<br />

The first line sets a Matrix object to the existing transformation matrix used by the myDisplayObject display object<br />

(the matrix property of the transformation property of the myDisplayObject display object). This way, the Matrix<br />

class methods that you call have a cumulative effect on the display object’s existing position, scale, and rotation.<br />

Note: The ColorTransform class is also included in the flash.geometry package. This class is used to set the<br />

colorTransform property of a Transform object. Since it does not apply any geometrical transformation, it is not<br />

discussed, in detail, here. For more information, see the ColorTransform class in the ActionScript 3.0 Refer<strong>en</strong>ce for the<br />

Adobe Flash Platform.<br />

Geometry example: Applying a matrix transformation<br />

to a display object<br />

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

The DisplayObjectTransformer sample application shows a number of features of using the Matrix class to transform<br />

a display object, including the following:<br />

Rotating the display object<br />

Scaling the display object<br />

Translating (repositioning) the display object<br />

Skewing the display object<br />

Last updated 6/6/2012<br />

216

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

Saved successfully!

Ooh no, something went wrong!