Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache flex Flex/ActionScript-围绕其中心旋转精灵_Apache Flex_Flash_Actionscript 3 - Fatal编程技术网

Apache flex Flex/ActionScript-围绕其中心旋转精灵

Apache flex Flex/ActionScript-围绕其中心旋转精灵,apache-flex,flash,actionscript-3,Apache Flex,Flash,Actionscript 3,我在Actionscript中创建了一个精灵,并将其渲染到Flex画布上。假设: var fooShape:Sprite = new FooSpriteSubclass(); fooCanvas.rawChildren.addChild(myshape); //Sprite shape renders on screen fooShape.rotation = fooNumber; 这将旋转我的形状,但似乎围绕左上角旋转 其父容器(画布)的点 如何强制精灵围绕自己的中心点旋转?我当然可以

我在Actionscript中创建了一个精灵,并将其渲染到Flex画布上。假设:

var fooShape:Sprite = new FooSpriteSubclass();

fooCanvas.rawChildren.addChild(myshape);

//Sprite shape renders on screen

fooShape.rotation = fooNumber;
这将旋转我的形状,但似乎围绕左上角旋转 其父容器(画布)的点

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

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


多谢各位

如果要围绕中心旋转,只需通过将内部资源x和y设置为资源宽度和高度的一半,将资源置于精灵内部的中心即可。这将使内容居中,并允许其围绕中心点旋转

运行时加载的资产示例如下:

var loader:Loader = new Loader():
var request:URLRequest = new URLRequest(path/to/asset.ext);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onLoaderComplete);
loader.load(request);

private function _onLoaderComplete(e:Event):void
{
    var mc:MovieClip = e.target.content as MovieClip;
    mc.x = -mc.width * 0.5;
    mc.y = -mc.height * 0.5;
    mc.rotation = 90;
    addChild(mc);
}

基于参考点旋转对象(使用object和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)); 
    


    使用的主要方法:


      • 在其他例子中运气不太好。这个对我有用。我在一个UIC组件上使用了它


        }

        事实上,我必须添加此代码,以使上述解决方案适合我

        private var _rotateCount = 0;
        var _origginalMatrix:Matrix=new Matrix();
        .........
        if (_rotateCount++ >= 360 / angleDegrees)
        {
            myObject.transform.matrix = _origginalMatrix;
            _rotateCount = 0;
                    return;
        }
        
        var matrix:Matrix = myObject.transform.matrix;
        .... 
        

        在长时间旋转后,对象会慢慢移动到右上角。

        另一种解决方案是将对象放在另一个视图中,移动对象,使图像中心位于容器的左上角,然后旋转容器

        import spark.components.*;
        
        var myContainer:View = new View();
        var myImage:Image = new Image();
        
        myContainer.addElement(myImage);
        myImage.x = myImage.width / -2;
        myImage.y = myImage.height / -2;
        
        addElement(myContainer);
        
        myContainer.rotation = whateverAngle;
        
        一个问题可能是,在创建图像时,图像的宽度不知道,因此您可能希望找到一种解决方法。(硬编码,或者查看myImage.preliminaryWidth是否有效)

        用法:

        您可以按如下所述使用上述功能

        var img:Canvas = new Canvas()
        RotateAroundCenter(img, rotation);
        
        这对你有帮助


        REf:

        这不是要求的。如果有人(比如我)不清楚,最好将deg-to-rad转换参数化。例如:rad_angle=angle*(Math.PI/180)此外,还可以缓存该系数以提高速度。同样,重用矩阵也是可行的,但我需要将矩阵重新设计到对象。(至少在html5和neko上)你的例子效果很好,看看我的其他评论,也许你的不起作用,因为你需要重新评估矩阵。
        /**
        * Rotates the object based on its center
        * Parameters: @obj => the object to rotate
        * @ rotation => angle to rotate
        * */
        public function RotateAroundCenter(obj:Object, rotation:Number):void
        {
        
        var bound:Rectangle = new Rectangle();
        // get the bounded rectangle of objects
        bound = obj.getRect(this);
        
        // calculate mid poits
        var midx1:Number = bound.x + bound.width/2;
        var midy1:Number = bound.y + bound.height/2;
        
        // assign the rotation
        obj.rotation = rotation;
        
        // assign the previous mid point as (x,y)
        obj.x = midx1;
        obj.y = midy1;
        
        // get the new bounded rectangle of objects
        bound = obj.getRect(this);
        
        // calculate new mid points
        var midx2:Number = bound.x + bound.width/2;
        var midy2:Number = bound.y + bound.height/2;
        
        // calculate differnece between the current mid and (x,y) and subtract
         //it to position the object in the previous bound.
        var diff:Number = midx2 - obj.x;
        obj.x -= diff;
        diff = midy2 - obj.y;
        obj.y -= diff;
        }
        
        //////////////////
        
        var img:Canvas = new Canvas()
        RotateAroundCenter(img, rotation);