# How to center a rotation in AS3

• 07-13-2008, 02:12 PM
kitsunegari
How to center a rotation in AS3
Hi

I have searched for hours for the solution on google and forums and I still can't figure out how to center a rotation on movieclip in AS3.

As you can see in my code, I'm trying to adjust on the fly the x and y properties so that the movieclip gives the impression to rotate from its center and not from its corner at 0,0
Code:

```var myclip:MovieClip = new MovieClip(); myclip.buttonMode = true; myclip.graphics.beginFill(0); myclip.graphics.drawRect(150, 150, 100, 100); addChild(myclip); myclip.addEventListener(MouseEvent.CLICK,rotate); function rotate(e:Event){         myclip.rotation += 2;         myclip.x = (stage.width/2) - (myclip.width/2);         myclip.y = (stage.height/2) - (myclip.height/2); }```
but it doesn't work and the clip does not rotate well :/

Please help me if you have any readymade snippet to make a clip rotate from its center or have any idea of what I should modify in my code. I'm kind of lost
Thanks
• 07-13-2008, 07:13 PM
cancerinform
• 07-14-2008, 06:22 AM
kitsunegari
awesome, thanks a lot
• 07-14-2008, 07:05 AM
flash_gamer
Thank You a lot

http://www.penalty08.ch
• 07-14-2008, 10:50 AM
kitsunegari
I have a question concerning the use of the matrix object

The following code works well to rotate the sprite from its center if its x and y properties don't change.

However, if I drag and drop the clip somewhere else after having rotated it, and then add a new rotation, it keeps rotating from the previous rotation point. Even if I add point =new Point(myspr.x+myspr.width/2, myspr.y+myspr.height/2); in a function that is called at the end of the drag and drop, the rotation point remains the same.

So, do you have any idea how to update the position of the matrix rotation point if the x and the y of the clip change on the scene after a rotation? (I hope my question is clear enough)
Thanks

Code:

```var myspr=new Sprite(); myspr.x=100; myspr.y=100; addChild (myspr); var point:Point=new Point(myspr.x+myspr.width/2, myspr.y+myspr.height/2); rotateAroundCenter(myspr,45); function rotateAroundCenter (ob:*, angleDegrees) {     var m:Matrix=ob.transform.matrix;     m.tx -= point.x;     m.ty -= point.y;     m.rotate (angleDegrees*(Math.PI/180));     m.tx += point.x;     m.ty += point.y;     ob.transform.matrix=m; }```
• 04-21-2011, 07:45 AM
25.pram
Thanks for sharing this code.

It works fine when I run this code in enter frame.

But it is not properly working when I am using slider component for rotating it.

I am passing the values between -360 to 360.

I am using AS3.