[英]How do I add an image/texture to my table in libGDX?
I made a table for a simple HUD which displays health, score and the current weapon from the player.我为一个简单的 HUD 制作了一个表格,它显示了玩家的生命值、分数和当前武器。 The weapon should be displayed as an image, but my program crashes if I try to do so.
武器应该显示为图像,但如果我尝试这样做,我的程序会崩溃。 It works fine if I use a number, so there is no error in my code.
如果我使用数字,它工作正常,所以我的代码没有错误。
public class Hud {
public Stage stage;
private Viewport viewport;
private int health;
private int score;
private Texture weaponImage;
Label healthLabel;
Label scoreLabel;
Label weaponLabel;
public Hud(SpriteBatch spriteBatch) {
health = 100;
score = 0;
weaponImage = new Texture(Gdx.files.internal("sword.png"));
viewport = new FitViewport(MyProject.V_HEIGHT, MyProject.V_HEIGHT, new OrthographicCamera());
stage = new Stage (viewport, spriteBatch);
Table table = new Table();
table.top();
table.setFillParent(true);
healthLabel = new Label(String.format("%03d", health), new Label.LabelStyle(new BitmapFont(), Color.WHITE));
scoreLabel = new Label(String.format("%03d", score), new Label.LabelStyle(new BitmapFont(), Color.WHITE));
//Here should be the code to display the image
table.add(healthLabel).expandX().padTop(10);
table.add(scoreLabel).expandX().padTop(10);
stage.addActor(table);
}
}
Found out how to do it: Instead of a texture, I created an image.了解如何做到这一点:我创建了一个图像,而不是纹理。
weaponImg = new Image(new Texture(Gdx.files.internal("sword.png")));
It worked fine when I added the following code in the HUD method:当我在 HUD 方法中添加以下代码时,它运行良好:
table.add(weaponImg).width(50).height(100);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.