3

当我在 Flex 容器中有一个对象(绝对定位)并将其比例值设置得非常低,然后旋转它,随着比例的减小,旋转变得“更不稳定”。我包括重现问题的代码。您可以点击“向上箭头”以缩小对象(“向下”以放大)和“向上”以增加对象的大小,以便您仍然可以看到它。缩小后,您可以使用“左”和“右”来旋转它。请注意,当对象未按比例缩小时,它会干净地旋转,但如果您将对象按比例缩小(同时增加对象的大小以便您仍然可以看到它),则旋转会变得不稳定。有任何想法吗?

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:test="com.test.*"
                   resize="onResize()" addedToStage="onStart()" removedFromStage="onEnd()">
<fx:Script>
    <![CDATA[
        [Bindable]
        private var _rotation:Number = 0;

        [Bindable]
        private var _scale:Number = 1;

        [Bindable]
        private var _blockSize:Number = 100;

        protected function onStart():void {
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
        }

        protected function onEnd():void {
            stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
        }

        protected function onResize():void {
            if (Layer) {
                Layer.x = width/2
                Layer.y = height/2;
            }
        }

        protected function onKeyDown(event:KeyboardEvent):void {
            if (event.keyCode == Keyboard.LEFT) {
                Layer.rotation += -1;
            }
            if (event.keyCode == Keyboard.RIGHT) {
                Layer.rotation += 1;
            }
            if (event.keyCode == Keyboard.UP) {
                Layer.scaleX = Layer.scaleY = (Layer.scaleX / 2);
            }
            if (event.keyCode == Keyboard.DOWN) {
                Layer.scaleX = Layer.scaleY = (Layer.scaleX * 2);
            }
            if (event.keyCode == Keyboard.PAGE_UP) {
                _blockSize *=2;
            }
            if (event.keyCode == Keyboard.PAGE_DOWN) {
                _blockSize /=2;
            }
            this._rotation = Layer.rotation;
            this._scale = Layer.scaleX;
        }
    ]]>
</fx:Script>
<s:SkinnableContainer id="Layer">
    <test:RotatedComponent width="{_blockSize}" height="{_blockSize}"/>
</s:SkinnableContainer>
<s:Label x="10" y="10" text="Rotation: {_rotation}"/>
<s:Label x="10" y="30" text="Scale: {_scale}"/>
<s:Label x="10" y="50" text="Block: {_blockSize}"/>
</s:WindowedApplication>

RotatedComponent.mxml 的代码

package com.test {

import mx.core.UIComponent;

public class RotatedComponent extends UIComponent {

    public function RotatedComponent() {
        super();
    }

    override public function set width(value:Number):void {
        super.width = value;
        this.x = - (width/2);
    }

    override public function set height(value:Number):void {
        super.height = value;
        this.y = - (height/2);
    }

    override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
        graphics.lineStyle(2, 0x333333);
        graphics.beginFill(0x660000);
        graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
        graphics.endFill();
        graphics.lineStyle(2, 0xffffff);
        graphics.drawCircle(unscaledWidth/2, unscaledHeight/2, 5);
    }
}
}

谢谢!德里克

4

1 回答 1

1

我认为这可以正式称为错误。如果我计算转换的矩阵而不是设置 component.scale() 和 component.rotation() 结果是正确的。我最好的猜测是 Flex 设置器下面的代码中的某处存在舍入错误(我单步执行了所有 Flex 代码,没有发生任何奇怪的事情)。所以简短的回答:

代替:

component.scaleX = component.scaleY = _scale;
component.rotation = _rotation;

利用

var matrix:Matrix = new Matrix();
matrix.scale(_scale, _scale);
matrix.rotate(_rotation * Math.PI / 180);
component.transform.matrix = matrix;

希望有帮助!德里克

于 2013-02-22T15:04:10.643 回答