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

标签 apache-flex flash actionscript-3

我在 Actionscript 中创建了一个 Sprite 并将其渲染为 Flex Canvas。认为:

var fooShape:Sprite = new FooSpriteSubclass();

fooCanvas.rawChildren.addChild(myshape);

//Sprite shape renders on screen

fooShape.rotation = fooNumber;

这将旋转我的形状,但似乎围绕左上角旋转
其父容器( Canvas )的点。

如何强制 Sprite 围绕自己的中心点旋转?我显然可以
编写代码来计算旋转,然后重新渲染,但我认为有
必须是一种内置的方式来做到这一点,当然不想“重新发明轮子”
如果可能的话。

我正在使用 FlexBuilder,因此无法访问完整的 Flash API。

非常感谢!

最佳答案

基于引用点旋转对象需要以下步骤(使用 Matrix 对象和 getBounds):

  • 矩阵平移(移动到引用点)
  • 矩阵旋转
  • 矩阵平移(回到原来的位置)

  • 例如,将对象围绕其中心旋转 90 度:
    // 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)); 
    

    使用的关键方法:
  • Matrix.rotate
  • Matrix.translate
  • DisplayObject.getBounds
  • 关于apache-flex - Flex/ActionScript - 围绕其中心旋转 Sprite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/993445/

    相关文章:

    java - 开源基于网络的屏幕共享

    actionscript-3 - AS3 : Exit desktop flash player

    c# - 如何编写基于网络浏览器的多人游戏?

    flash - as3 : youtube api/possible to add youtube's "like-button" (rating)?

    javascript - 通过.swf文件从网页中获取标题标签的内容

    apache-flex - 弹性 : Text Input that accepts number only

    html - flex 的预期生命周期长吗

    apache-flex - 带有配置文件的条件 Maven 构建部分?

    flash - Flash Packager for IOS:编译后的应用程序的性能是否太差而无法使用?

    PHP/Actionscript 3 带有进度指示器的多个文件上传