A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: How to center a rotation in AS3

  1. #1
    Member
    Join Date
    Jan 2008
    Posts
    31

    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

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Member
    Join Date
    Jan 2008
    Posts
    31
    awesome, thanks a lot

  4. #4

  5. #5
    Member
    Join Date
    Jan 2008
    Posts
    31
    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;
    }
    Last edited by kitsunegari; 07-14-2008 at 10:57 AM.

  6. #6
    Junior Member
    Join Date
    Aug 2006
    Posts
    1
    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.

    Please help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center