【问题标题】:Rotate TextButton with libgdx使用 libgdx 旋转 TextButton
【发布时间】:2013-07-26 09:56:01
【问题描述】:

我有一个 textButton,我想把它放在旋转 90 度的屏幕上。

由于某些原因,与 TextButton 对象关联的所有旋转(rotate()、setRotationAngle() 等)方法都无法正常工作。

所以我实现了扩展 TextButton 并覆盖 draw() 方法的新类:

@Override
public void draw(SpriteBatch batch, float parentAlpha) {
     Matrix4 rotationMatrix = new Matrix4();
     Matrix4 oldMatrix = batch.getTransformMatrix();
     rotationMatrix.idt();
     rotationMatrix.rotate(new Vector3(this.getX(),this.getY()+this.getHeight(),0),rotationAngle);
     batch.setTransformMatrix(rotationMatrix);
     super.draw(batch, parentAlpha);
     batch.setTransformMatrix(oldMatrix);
}

其中rotationAngle 等于 90.0。并且由于某种原因,按钮没有旋转 90 度,而是旋转了一些未知的度数。

UPD

在我切换回 TextButton 对象并做了之后:

newGame.setTransform(true);
newGame.rotate(90);

这意味着按钮中的文本已正确旋转,但按钮的背景保持在原位:

所以我的问题是:为什么会发生这种情况,我该如何解决?

【问题讨论】:

    标签: android libgdx matrix-transform


    【解决方案1】:

    我根据documentation实现了旋转Widgets

    这是我的代码:

    Table buttonContainer = new Table(skin);
    buttonContainer.setTransform(true);
    buttonContainer.add(button1);
    buttonContainer.row().pad(10);
    buttonContainer.add(button2);
    rotatingActor = buttonContainer;
    

    然后:

    rotatingActor.setRotation(newDegree);
    

    即使小部件旋转,所有点击处理程序等也会按预期工作。

    【讨论】:

      【解决方案2】:

      有一个 project issue 因无法修复而关闭。

      scene2d 中的所有 UI 元素都不能旋转,因为剪切是通过剪刀实现的。剪裁需要轴对齐的矩形。

      【讨论】:

      • 好的,但这并不意味着不能实现矩阵变换的旋转。
      【解决方案3】:

      现有 Actors 的旋转方法应该可以工作。可能值得提出另一个问题来追踪该问题。

      我发现至少有两个问题:

      1. 默认的批量变换矩阵可能不是单位矩阵。也许将rotationMatrix 初始化为oldMatrix 的副本?

      2. 您正在围绕一个非常任意的矢量旋转(从原点画一条线——在按钮的左下角到左上角)。试试Vector3(0, 1, 0)

      【讨论】:

      • 与其他向量的旋转没有帮助。但是 TextButton 对象的方法 setTrasform() 有助于旋转按钮的文本,而不是按钮本身(P.S 我已经更新了问题)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-02
      • 2017-12-30
      • 2015-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多