2014-02-07 114 views
3

我想每隔x秒改變菜單的背景圖片。我正在使用libGDX scene2D.ui來製作菜單。 TestScreen類擴​​展了AbstractScreen,它是一個實現libGDX Screen類的抽象類。 問題:在通過堆棧中的Table對象將圖像加載到舞臺後,將圖像引用更改爲不同的圖像不起作用。 Stage.draw()不關心它,就好像它複製了我的原始圖像。我想保持背景爲Image類並通過stage.draw()渲染。libgdx背景圖片變化

爲了進一步複雜化,如果我在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); 
} 

}

+0

嘗試調用Table.invalidate()。 – Viacheslav

+0

試過這個,它沒有工作。我在BackGroundLayer中創建了一個類對象,並在render方法中更改了Image之後調用了.invalidate()方法。下面的解決方案很好地工作:) – Artash

回答

6

ImageActor一個子類。主要區別在於,Image裏面有Drawable。如果您撥打stage.draw(),則Drawable會被吸引,該號碼將調用Imagedraw()。您可以使用setDrawable(Drawable param);更改Drawable,而不是更改Image。 什麼是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)]); 
    } 
} 

這應該改變形象後,工作

+1

這工作非常好!謝謝 :) – Artash