我正在为使用LibGDX进行Android开发的游戏开发一个主菜单屏幕。它需要三个ImageTextButtons - 播放,设置,注销。这三个按钮中的每一个都有一个与之关联的不同图像,但是,当我运行应用程序时,每个按钮都有相同的图像 - 注销图像。我想这可能与注销按钮是最后一个按钮的事实有关,所以它可能会覆盖其他按钮的图像吗?
如您所见,即使我为每个按钮加载并使用了不同的图像文件,所有按钮上也会显示相同的注销图标。以下是构建和初始化按钮的代码:
private ImageTextButton playBtn, settingsBtn, logoutBtn;
public void initButtons(){
playBtn = new ImageTextButton("Play", skin, "default");
playBtn.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("playBtn.png"))));
playBtn.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("playBtn.png"))));
settingsBtn = new ImageTextButton("Settings", skin, "default");
settingsBtn.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("settingsBtn.png"))));
settingsBtn.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("settingsBtn.png"))));
logoutBtn = new ImageTextButton("Log Out", skin, "default");
logoutBtn.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("logoutBtn.png"))));
logoutBtn.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("logoutBtn.png"))));
}
这是show方法,我在其中创建/加载外观并调用方法来初始化按钮:
@Override
public void show() {
Gdx.input.setInputProcessor(stage);
this.skin = new Skin();
this.skin.addRegions(MathsVsZombies.manager.get("UI/uiskin.atlas", TextureAtlas.class));
this.skin.add("default-font", MathsVsZombies.font24);
this.skin.load(Gdx.files.internal("UI/uiskin.json"));
initButtons();
}
为了让ImageTextButtonStyle工作,我必须将此代码添加到皮肤JSON文件中:
"com.badlogic.gdx.scenes.scene2d.ui.ImageTextButton$ImageTextButtonStyle":{
"default": { "down": "default-round-down", "up": "default-round", "font": "default-font", "fontColor": "white" }
},
"默认"的代码相同TextButton的样式。也许这就是问题所在?我很肯定图像文件存储在正确的目录下,并且它们都存储在一起。这就是他们应该看起来的样子:
请帮助解决这个问题,因为我对LibGDX和gamedev的了解非常有限,所以我不知道出了什么问题,更不用说如何修复它了。
答案 0 :(得分:2)
Skin将为每个按钮使用相同的样式实例,因此当您使用getStyle()并更新drawable时,将更新使用该样式的每个按钮。
通常,您为皮肤本身的每个不同按钮类型指定一个单独的样式,但如果您愿意,也可以在更改drawable之前复制该样式。
答案 1 :(得分:0)
我通过在皮肤JSON文件中创建三个单独的样式来修复此问题,每个特定按钮一个。
"com.badlogic.gdx.scenes.scene2d.ui.ImageTextButton$ImageTextButtonStyle":{
"play": { "down": "default-round-down", "up": "default-round", "font": "default-font", "fontColor": "white" },
"settings": { "down": "default-round-down", "up": "default-round", "font": "default-font", "fontColor": "white" },
"logout": { "down": "default-round-down", "up": "default-round", "font": "default-font", "fontColor": "white" }
然后在initButtons方法中,我将每个样式更改为JSON文件中的相应样式。这样,我为每个按钮加载的每个图像文件都存储为该特定样式(这就是为什么你需要三个单独的样式,否则最近加载的文件将是与“默认”样式相关联的文件)。
playBtn = new ImageTextButton("Play", skin, "play");
playBtn.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("playBtn.png"))));
playBtn.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("playBtn.png"))));
settingsBtn = new ImageTextButton("Settings", skin, "settings");
settingsBtn.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("settingsBtn.png"))));
settingsBtn.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("settingsBtn.png"))));
logoutBtn = new ImageTextButton("Log Out", skin, "logout");
logoutBtn.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("logoutBtn.png"))));
logoutBtn.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("logoutBtn.png"))));