2014-02-07 5 views
3

x 초마다 메뉴의 배경 이미지를 변경하고 싶습니다. 나는 메뉴를 만들기 위해 libGDX scene2D.ui를 사용하고있다. TestScreen 클래스는 libGDX의 Screen 클래스를 구현하는 추상 클래스 인 AbstractScreen을 확장합니다. 문제점 : 스택의 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

시도를 작동합니다. – Viacheslav

+0

시도해 보니 작동하지 않았습니다. BackGroundLayer를 클래스 객체로 만들고 render 메서드에서 Image를 변경 한 후 .invalidate() 메서드를 호출했습니다. 아래의 솔루션은 멋지게 작동했습니다 :) – Artash

답변

6

ImageActor의 서브 클래스이다. 가장 큰 차이점은 Image의 내부에 Drawable이 있다는 것입니다. 이 Drawablestage.draw()으로 전화하면 draw()Image입니다. Image을 변경하는 대신 setDrawable(Drawable param);을 사용하여 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)]); 
    } 
} 

이 이미지를 변경 한 후() Table.invalidate를 호출 할

+1

이것은 매우 잘 작동했습니다! 고맙습니다 :) – Artash