缩放Flex对象会导致旋转异常

时间:2013-02-21 18:33:57

标签: flex actionscript rotation scale

当我在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);
    }
}
}

谢谢! 德里克

1 个答案:

答案 0 :(得分:1)

我认为这可以正式称为错误。如果我计算转换的矩阵而不是设置component.scale()和component.rotation(),结果是正确的。我最好的猜测是,在Flex setter下面的代码中存在舍入错误(我逐步完成了所有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;

希望有所帮助! 德里克