且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Flex/ActionScript - 围绕其中心旋转 Sprite

更新时间:2023-02-02 17:46:18

根据参考点旋转对象需要以下步骤(使用 Matrix 对象和 getBounds):

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

  1. 矩阵平移(移动到参考点)
  2. 矩阵旋转
  3. 矩阵平移(回到原来的位置)


例如,将对象围绕其中心旋转 90 度:


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: