的Flex /动作 - 雪碧旋转围绕其中心雪碧、动作、中心、Flex

2023-09-08 13:16:00 作者:鲁童

我创建了一个雪碧在ActionScript和渲染到一个Flex的画布。假设:

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

var fooShape:Sprite = new FooSpriteSubclass();

fooCanvas.rawChildren.addChild(myshape);

//Sprite shape renders on screen

fooShape.rotation = fooNumber;

这将转动我的外形,但似乎其围绕左上角 点其父容器(画布)的。

This will rotate my shape, but seems to rotate it around the upper-left point of its parent container(the canvas).

我怎样才能迫使雪碧转动的是自己的心呢?我能明显 写code来计算的旋转,然后我们会重新渲染,但我认为 必须有一个内置的方式做到这一点,当然不希望推倒重来 如果可能的话。

How can I force the Sprite to rotate about is own center point? I could obviously write code to calculate the rotation, and then have it re-render, but I think there must be a built-in way to do this, and certainly do not want to 'reinvent the wheel' if possible.

我使用弹性,因此不必访问完整的Flash API。

I am using FlexBuilder, and therefore do not have access to the full Flash API.

感谢您了!

推荐答案

需要执行以下步骤基于一个参考点旋转对象(使用的矩阵对象的getBounds):

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

在矩阵转换(移动到参考点) 矩阵旋转 在矩阵转换(回到原始位置)

例如,要旋转一个物体围绕中心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:

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