Mirror movieclips and sprites using AS3

One thing I had a hard time getting my head around when I started learning actionscript was how to use the matrixclass. Most of the time I needed to do quite simple stuff like flipping a movieclip around its own axis. And since I needed to do it again a few days ago I thought ”why not post the code so that other newbies can see”.

This is just the code cut from the project and exported as a single fla-file but you get the idea.

Demo of the ”movieclip mirror thingy” and the mirror.fla.

holder.addEventListener(MouseEvent.CLICK,mirror);

function mirror(e:MouseEvent):void{

// Set up the matrix that will transform the bitmap later on. This one will flip horizontal...
var matrix : Matrix = new Matrix( -1, 0, 0, 1, holder.width, 0 );

// ...and this one vertical...
//	var matrix : Matrix = new Matrix( 1, 0, 0, -1, 0, holder.height );

// ...and this one will do both.
//	var matrix : Matrix = new Matrix( -1, 0, 0, -1, holder.width, holder.width );

// Create a new BitmapData with the same size as "holder".
var mirror : BitmapData = new BitmapData( holder.width,holder.height );

// Copy the visual content of "holder" and let the matrix do its magic.
mirror.draw( holder, matrix );

// We create a new bitmap and tell it to use the BitmapData "mirror" as source.
var image:Bitmap = new Bitmap(mirror);

// Unless we remove the child at level 0 the holderclip will eat ram if we mirror the holderclip alot since we use bitmaps.
holder.removeChildAt(0);

// Add the new bitmap to "holder".
holder.addChild(image);
}

Kommentarer



*