AS3 围绕其中心点旋转对象 [英] AS3 Rotate an object around its center point

查看:35
本文介绍了AS3 围绕其中心点旋转对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这个对象围绕它的中心而不是左上角旋转.代码如下所示:

I want this object to rotate around its center rather than the top left corner. The code looks like this:

        switch (event.keyCode)
        {
            case 37:
            car.rotation = -90;
               car.x -= 5;
               break;

所以当我按下左键时,汽车向左转,但现在它跳起来了一点,因为它绕着顶角旋转.

So when i press the left key, the car turns left but as it is now it jumps up a bit because its rotating around the top corner.

谢谢

推荐答案

以下内容将围绕中心旋转:

The following will rotate around center :

public function rotateAroundCenter(object:DisplayObject, angleDegrees:Number):void {
    if (object.rotation == angleDegrees) {
        return;
    }
        
    var matrix:Matrix = object.transform.matrix;
    var rect:Rectangle = object.getBounds(object.parent);
    var centerX = rect.left + (rect.width / 2);
    var centerY = rect.top + (rect.height / 2);
    matrix.translate(-centerX, -centerY);
    matrix.rotate((angleDegrees / 180) * Math.PI);
    matrix.translate(centerX, centerY);
    object.transform.matrix = matrix;
    
    object.rotation = Math.round(object.rotation);
}
    

它将对象的中心平移到 0,0,然后旋转它,然后将其平移回来.

It translates the center of the object to 0,0 then rotate it and then translate it back.

这篇关于AS3 围绕其中心点旋转对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆