【问题标题】:Libgdx rotating imagebuttonLibgdx 旋转图像按钮
【发布时间】:2018-07-04 22:54:16
【问题描述】:

我正在尝试将图像按钮旋转 90 度。当我调用button.setRotation(90) 时,它会旋转按钮,但不会旋转与按钮关联的图像。我知道实际的按钮是旋转的,因为我在单击它时会打印出 true ,并且它会感应到按钮应该旋转 90 度的单击。但是,设置按钮的位置确实会移动按钮和图像。如何确保图像随按钮旋转?谢谢!

package com.davejones.test;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.utils.viewport.ExtendViewport;


public class Test extends ApplicationAdapter {

Stage stage;
ImageButton button;
ImageButton.ImageButtonStyle imageButtonStyle;
BitmapFont font;
Skin skin;
TextureAtlas textureAtlas;
ExtendViewport viewport;

@Override
public void create () {
    stage = new Stage();
    skin = new Skin();
    viewport = new ExtendViewport(64,48);
    viewport.getCamera().position.set(32, 24, 0);
    textureAtlas = new TextureAtlas("image.pack");
    skin.addRegions(textureAtlas);
    Gdx.input.setInputProcessor(stage);
    font = new BitmapFont();
    imageButtonStyle = new ImageButton.ImageButtonStyle();
    imageButtonStyle.imageUp = skin.getDrawable("image");
    button = new ImageButton(imageButtonStyle);
    button.setPosition(100, 100);
    button.setRotation(90);
    stage.addActor(button);
}


@Override
public void render () {
    System.out.println(button.isPressed());
    super.render();
    stage.draw();
}

@Override
public void dispose () {
    stage.dispose();
    skin.dispose();
    font.dispose();
    textureAtlas.dispose();
}

}

【问题讨论】:

标签: java android libgdx scene2d


【解决方案1】:

您需要将按钮的转换设置为 true:

...
button.setTransform(true);
button.setRotation(90);
...

来自Libgdx Group docs

public void setTransform(布尔变换)

当为真(默认)时, Batch 被转换,所以孩子们被画在他们父母的 坐标系。这会影响性能,因为 Batch.flush() 必须在变换前后进行。如果一组演员 不旋转或缩放,则可以设置组的变换 为假。在这种情况下,每个孩子的位置将被 组的绘图位置,导致孩子出现在 即使 Batch 尚未转换,位置也正确。

所以请注意可能的性能影响,如果您绘制了很多将变换设置为 true 的组。

【讨论】:

  • 不确定是否有帮助。 当为真(默认)时...。如您所见,默认情况下它是真的。
  • 用于 Group 类。对于 ImageButton,默认情况下为 false
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-21
  • 1970-01-01
  • 2012-03-15
相关资源
最近更新 更多