Flex/ActionScript - rotate Sprite around its center

前端 未结 6 1526
一向
一向 2021-02-07 13:15

I have created a Sprite in Actionscript and rendered it to a Flex Canvas. Suppose:

var fooShape:Sprite = new FooSpriteSubclass();

fooCanvas.rawChildren.addChild         


        
6条回答
  •  忘了有多久
    2021-02-07 14:16

    The following steps are required to rotate objects based on a reference point (using Matrix object and getBounds):

    1. Matrix translation (moving to the reference point)
    2. Matrix rotation
    3. Matrix translation (back to original position)


    For example to rotate an object 90 degrees around its center:

    // Get the matrix of the object  
    var matrix:Matrix = myObject.transform.matrix; 
    
    // Get the rect of the object (to know the dimension) 
    var rect:Rectangle = myObject.getBounds(parentOfMyObject); 
    
    // Translating the desired reference point (in this case, center)
    matrix.translate(- (rect.left + (rect.width/2)), - (rect.top + (rect.height/2))); 
    
    // Rotation (note: the parameter is in radian) 
    matrix.rotate((90/180)*Math.PI); 
    
    // Translating the object back to the original position.
    matrix.translate(rect.left + (rect.width/2), rect.top + (rect.height/2)); 
    



    Key methods used:

    • Matrix.rotate
    • Matrix.translate
    • DisplayObject.getBounds

提交回复
热议问题