Friday, December 26, 2008

As3 flip Horizontal / flip Vertical a displayObject

public function flipHorizontal(dsp:DisplayObject):void
{
var matrix:Matrix = dsp.transform.matrix;
matrix.a=-1;
matrix.tx=dsp.width+dsp.x;
dsp.transform.matrix=matrix;
}

public function flipVertical(dsp:DisplayObject):void
{
var matrix:Matrix = dsp.transform.matrix;
matrix.d=-1;
matrix.ty=dsp.height+dsp.y;
dsp.transform.matrix=matrix;
}

Resource: http://www.dreaminginflash.com/as3-flip-horizontal-flip-vertical-a-displayobject/

2 comments:

Rem0teMeth0d said...

If you call the method 3 times in a row for the same DisplayObject,
it doesn't just flip it translates the DisplayObject as well. In a real world example, attach this method to a check box that flips the display object on each select and flips it back on unselect. Now you do it three times and you will notice that the image has translated.

Here is what you can do to make a fix, I am sure you must have figured that out already, so am i adding it for the reference of other users who might encounter the same.

in flipHorizontal:
...
matrix.a= mitrix.a * -1;
if(mitrix.a < 0) {
matrix.tx=dsp.width-dsp.x;
} else {
matrix.tx=dap.x-dsp.width;
}
...

in flipVertical:
...
matrix.d = matrix.d * -1;
if(matrix.d < 0) {
matrix.ty = dsp.height - dsp.y;
} else {
matrix.ty = dsp.y - dsp.height;
...

Don't forget to visit my blog and leave your feedback...
http://accidentalpatterns.blgospot.com

André Lelis Gonçalves said...

the flip whorks well...
but in my project it still translates the object..

a quick fix was to put a Math.Abs like the code below:

in flipHorizontal:
...
matrix.a= matrix.a * -1;
if (matrix.a < 0) {
matrix.tx=image.width-Math.abs(image.x);
} else {
matrix.tx = image.x - image.width;
}
...

in flipVertical:
...
matrix.d = matrix.d * -1;
if(matrix.d < 0) {
matrix.ty = dsp.height - Math.abs(dsp.y);
} else {
matrix.ty = dsp.y - dsp.height;
...

Hope it help someone