User Guide
424 Working with Geometry
Defining Matrix objects
Although you could define a matrix by directly adjusting the properties (a, b, c, d, tx, ty) of a
Matrix object, it is easier to use the
createBox() method. This method includes parameters
that let you directly define the scaling, rotation, and translation effects of the resulting matrix.
For example, the following code creates a Matrix object that has the effect of scaling an object
horizontally by 2.0, scaling it vertically by 3.0, rotating it by 45 degrees, moving (translating)
it 10 pixels to the right, and moving it 20 pixels down:
var matrix:Matrix = new Matrix();
var scaleX:Number = 2.0;
var scaleY:Number = 3.0;
var rotation:Number = 2 * Math.PI * (45 / 360);
var tx:Number = 10;
var ty:Number = 20;
matrix.createBox(scaleX, scaleY, rotation, tx, ty);
You can also adjust the scaling, rotation, and translation effects of a Matrix object by using the
scale(), rotate(), and translate() methods. Note that these methods combine with the
values of the existing Matrix object. For instance, the following code sets a Matrix object that
scales an object by a factor of 4 and rotates it 60 degrees, since the
scale() and rotate()
methods are called twice:
var matrix:Matrix = new Matrix();
var rotation:Number = 2 * Math.PI * (30 / 360); // 30°
var scaleFactor:Number = 2;
matrix.scale(scaleFactor, scaleFactor);
matrix.rotate(rotation);
matrix.scale(scaleX, scaleY);
matrix.rotate(rotation);
myDisplayObject.transform.matrix = matrix;
To apply a skew transformation to a Matrix object, adjust its b or c property. Adjusting the b
property skews the matrix vertically, and adjusting the
c property skews the matrix
horizontally. The following code skews the
myMatrix Matrix object vertically by a factor of 2:
var skewMatrix:Matrix = new Matrix();
skewMatrix.b = Math.tan(2);
myMatrix.concat(skewMatrix);
You can apply a Matrix transformation to the transform property of a display object. For
example, the following code applies a matrix transformation to a display object named
myDisplayObject:
var matrix:Matrix = myDisplayObject.transform.matrix;
var scaleFactor:Number = 2;
var rotation:Number = 2 * Math.PI * (60 / 360); // 60°
matrix.scale(scaleFactor, scaleFactor);