gpt4 book ai didi

apache-flex - 缩放 Flex 对象导致旋转行为异常

转载 作者:行者123 更新时间:2023-12-02 03:49:29 25 4
gpt4 key购买 nike

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

谢谢!德里克

最佳答案

我认为这可以正式称为错误。如果我计算转换矩阵而不是设置 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;

希望对您有所帮助!德里克

关于apache-flex - 缩放 Flex 对象导致旋转行为异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15009757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com