使用libgdx旋转TextButton

4

我有一个文本按钮,想把它旋转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度,而是以未知的角度旋转。

enter image description here

更新

在我切换回对象并执行以下操作后:

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

几乎成功了,这意味着按钮中的文本已正确旋转,但是按钮的背景仍然停留在原地: enter image description here 所以我的问题是:为什么会发生这种情况,我该如何解决?
3个回答

4
我根据文档实现了旋转小部件。
以下是我的代码:
Table buttonContainer = new Table(skin);
buttonContainer.setTransform(true);
buttonContainer.add(button1);
buttonContainer.row().pad(10);
buttonContainer.add(button2);
rotatingActor = buttonContainer;

然后:

rotatingActor.setRotation(newDegree);

所有的点击处理程序等在旋转小部件时都能按预期工作。

2

有一个项目问题被关闭,不会修复。

由于剪辑是通过剪切实现的,因此scene2d中的所有UI元素都无法旋转。剪辑需要轴对齐的矩形。


好的,但这并不意味着矩阵变换旋转不能被实现。 - Ricardo Simmus

1
现有演员的旋转方法应该有效。可能值得提出另一个问题来跟踪这个问题。
我看到至少两个问题:
1. 默认批处理变换矩阵可能不是单位矩阵。也许将`rotationMatrix`初始化为`oldMatrix`的副本?
2. 你正在围绕一个非常任意的向量旋转(从原点绘制一条线 - 从您的按钮左下角到左上角)。尝试使用`Vector3(0, 1, 0)`。

旋转与其他向量并没有帮助。但是TextButton对象的setTransform()方法可以帮助旋转按钮文本,但不能旋转按钮本身。(附言:我已更新问题) - Ricardo Simmus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接