我正在开发一个游戏,其中目录中的几张图片显示在屏幕上。
我正在使用 scene2d。
但由于某种原因这些图片没有显示在屏幕上。
任何人都可以解释为什么吗?
这是我的代码:
public class Picture extends Actor {
private Image img;
public Picture(Image img) {
this.img = img;
}
}
以及应该绘制图片的游戏类:
public class GameScreen extends Stage implements Screen {
private FileHandle dirWithTextures;
public GameScreen() {
super(new StretchViewport(1260.0f, 836.0f, new OrthographicCamera()));
dirWithTextures = Gdx.files.internal("textures/");
}
public void buildStage() {
ArrayList<Picture> pictureList = new ArrayList<Picture>();
for (int i = 0; i < 4; i++) {
pictureList.add(new Picture(new Image(newTexture(dirWithTextures.list()[i]))));
}
Collections.shuffle(pictureList);
for (Picture p: pictureList) {
p.setPosition(getWidth() / 2, getHeight() / 2, Align.center);
addActor(p);
}
}
}
最佳答案
您没有重写 Actor 的 draw() 方法,因此 Picture 不绘制任何内容。
像这样覆盖:
public class Picture extends Actor {
private Image img;
public Picture(Image img) {
this.img = img;
}
@Overrine
public void draw(Batch batch, float parentAlpha){
image.draw(batch, parentAlpha);
}
}
Actor 绘制方法:
public void draw (Batch batch, float parentAlpha) {
}
关于java - 如何在libGDX中显示图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47133379/