我想每 x 秒更改一次菜单的背景图像。我正在使用 libGDX scene2D.ui 来制作菜单。TestScreen 类扩展了 AbstractScreen 抽象类,它实现了 libGDX 的 Screen 类。问题:通过堆栈上的 Table 对象将图像加载到舞台后,将图像引用更改为不同的图像没有任何作用。Stage.draw() 毫不在意,就好像它复制了我的原始图像一样。我想将背景保留为 Image 类并通过 stage.draw() 进行渲染。
更复杂的是,如果我在 render() 方法中将图像更改为另一个图像,那么 image.setVisible(false) 也会停止工作。
public class TestScreen extends AbstractScreen {
private Stage stage;
private Image background;
private boolean ChangeBackground = true;
private final float refreshTime = 2.0f; // refresh to new image every 2 seconds.
private float counter = refreshTime;
public TestScreen(Game game) {
super(game);
}
@Override
public void render(float deltaTime) {
Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
if(ChangeBackground){
counter -= deltaTime;
if(counter < 0){
counter = refreshTime;
// Assets class has the 12 images loaded as "Image" objects already.
// I simple want to change the reference to other (already loaded in memory images) ...
// and make stage render the new image.
background = Assets.instance.wallpapers[(int) (Math.random()*12)]; // The image should change.
//background.setVisible(false);
}
}
stage.act(deltaTime);
stage.draw();
}
@Override
public void resize(int width, int height) {
stage.setViewport(Constants.VIEWPORT_GUI_WIDTH, Constants.VIEWPORT_GUI_HEIGHT, false);
}
@Override
public void show() {
stage = new Stage();
Gdx.input.setInputProcessor(stage);
makeStage();
}
@Override
public void hide() {
stage.dispose();
}
@Override
public void pause() {
}
// Builds the Background later and adds it to a stage through a stack.
// This is how it's done in my game. I made this test bench to demonstrate.
private void makeStage() {
Table BackGroundLayer = new Table();
background = Assets.instance.wallpapers[(int) (Math.random()*12)];
BackGroundLayer.add(background);
Stack layers = new Stack();
layers.setSize(800, 480);
layers.add(BackGroundLayer);
stage.clear();
stage.addActor(layers);
}
}