Using the matrix transformation in Flash AS3 to manipulate display objects (such as images, graphics, and animations) is very convenient for programmers. The following code demonstrates how to transpose, reverse, and flip any display object in Flash. The principle is simple: set the zoom value of the object to -1 in AS, where a represents the horizontal direction and b represents the vertical direction.
The code is as follows:
public class Transverse
{
public static function transLeftRight(obj : DisplayObject) :void
{
var mtx:Matrix = new Matrix();
mtx.a=-1; // Set a to -1
mtx.tx=obj.width; // Set translation
mtx.concat(obj.transform.matrix); // Concatenate matrices
obj.transform.matrix = mtx; // Transform
}
public static function transUpDown(obj : DisplayObject) :void
{
var mtx:Matrix = new Matrix();
mtx.b=-1; // Set b to -1
mtx.ty=obj.height; // Set translation
mtx.concat(obj.transform.matrix); // Concatenate matrices
obj.transform.matrix = mtx; // Transform
}
}
A brief introduction to the code:
// Define a new transformation matrix instance var mtx = new Matrix(); // Set a to -1 for horizontal transposition; // Set b to -1 for vertical transposition mtx.a=-1; // Set translation, if not set, the transposition will be in place // tx, ty can be set to the width and height of the object based on the transformation method mtx.tx=obj.width; // Concatenate matrices, combining the old transformation matrix of obj with mtx to get a new matrix // Before transposing, it is necessary to concatenate the old transformation matrix of obj, which can // preserve the matrix transformation performed on obj before transposing. mtx.concat(obj.transform.matrix); // Transform obj.transform.matrix = mtx;