Flex/ActionScript - rotate Sprite around its center

前端 未结 6 1537
一向
一向 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条回答
  •  猫巷女王i
    2021-02-07 14:09

    /**
    * Rotates the object based on its center
    * Parameters: @obj => the object to rotate
    * @ rotation => angle to rotate
    * */
    public function RotateAroundCenter(obj:Object, rotation:Number):void
    {
    
    var bound:Rectangle = new Rectangle();
    // get the bounded rectangle of objects
    bound = obj.getRect(this);
    
    // calculate mid poits
    var midx1:Number = bound.x + bound.width/2;
    var midy1:Number = bound.y + bound.height/2;
    
    // assign the rotation
    obj.rotation = rotation;
    
    // assign the previous mid point as (x,y)
    obj.x = midx1;
    obj.y = midy1;
    
    // get the new bounded rectangle of objects
    bound = obj.getRect(this);
    
    // calculate new mid points
    var midx2:Number = bound.x + bound.width/2;
    var midy2:Number = bound.y + bound.height/2;
    
    // calculate differnece between the current mid and (x,y) and subtract
     //it to position the object in the previous bound.
    var diff:Number = midx2 - obj.x;
    obj.x -= diff;
    diff = midy2 - obj.y;
    obj.y -= diff;
    }
    
    //////////////////
    

    Usage:

    you can use the above function as described below,

    var img:Canvas = new Canvas()
    RotateAroundCenter(img, rotation);
    

    This will help you

    REf: http://subashflash.blogspot.in/2010/08/rotation-of-object-based-on-center.html

提交回复
热议问题