LibGDX背景图片更换。

4
我想每隔x秒更改菜单的背景图像。我正在使用libGDX scene2D.ui创建菜单。TestScreen类扩展了AbstractScreen,它是一个抽象类,实现了libGDX的Screen类。 问题:通过堆栈上的Table对象将Image加载到舞台后,将图像引用更改为不同的图像时没有任何作用。 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);
}

}


在更改图像后尝试调用Table.invalidate()。 - Viacheslav
尝试过这个方法,但是没有成功。我将BackGroundLayer作为一个类对象,并在render方法中更改图像后调用了.invalidate()方法。不过下面的解决方案效果很好 :) - Artash
1个回答

7
ImageActor的一个子类。主要的区别在于,Image内部有一个Drawable。如果您调用stage.draw(),它会绘制这个Drawable,这个方法会调用Imagedraw()方法。您可以使用setDrawable(Drawable param);来更改Drawable,而不是更改Image本身。 什么是Drawable?任何实现Drawable接口的类都是Drawable,例如TextureRegionDrawable。如果您正在使用TextureRegion,可以使用这个构造函数:TextureRegionDrawable(TextureRegion region);。也许将背景图像存储在Drawable数组中会更好,这样您就不必每次设置新的Drawable时都调用构造函数。示例代码:
TextureRegionDrawable[] images = new TextureRegionDrawable[12];
for (int i = 0; i<12; i++) {
    images[i] = new TextureRegionDrawable(Assets.instance.textureRegions[i]);
}

然后在你的渲染函数中:

if(changeBackground) {
   counter -= delta;
   if (counter < 0) {
       counter = refreshtime
       background.setDrawable(images[(int)(Math.random()*12)]);
   }
}

这应该可以工作


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接