Flex/ActionScript - 围绕其中心旋转 Sprite [英] Flex/ActionScript - rotate Sprite around its center
问题描述
我在 Actionscript 中创建了一个 Sprite 并将其渲染为 Flex Canvas.假设:
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).
如何强制 Sprite 围绕自己的中心点旋转?我显然可以编写代码来计算旋转,然后重新渲染,但我认为有必须是一种内置的方式来做到这一点,当然不想重新发明轮子"如果可能的话.
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.
我使用的是 FlexBuilder,因此无法访问完整的 Flash API.
I am using FlexBuilder, and therefore do not have access to the full Flash API.
非常感谢!
推荐答案
根据参考点旋转对象需要以下步骤(使用 Matrix 对象和 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:
这篇关于Flex/ActionScript - 围绕其中心旋转 Sprite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!