2014-05-04 83 views
0

我有兩個clases,一個用於主要方法,另一個用於飛濺。但是,這兩者之間的切換不起作用。我想在Splash類中繪製圖像,但它瞬間變黑。當我將代碼從splash轉移到main類時,圖像出現了。在LibGDX中設置新屏幕

主要類:

public class Main extends Game { 
public static int WIDTH, HEIGHT; 

public void create() { 
    WIDTH = Gdx.graphics.getWidth(); 
    HEIGHT = Gdx.graphics.getHeight(); 
    setScreen(new Splash()); 
} 

public void render() { } 
public void dispose() { super.dispose(); } 
public void pause() { super.pause(); } 
public void resize(int width, int height) { super.resize(width, height); } 
public void resume() { } 
} 

飛濺類:

public class Splash implements Screen { 

private Sprite splash; 
private SpriteBatch sb; 

public void show() { 
    sb = new SpriteBatch(); 
    Texture splashTexture = new Texture(Gdx.files.internal("res/img/splash.png")); 
    splash = new Sprite(splashTexture); 
    splash.setSize(Main.WIDTH, Main.HEIGHT); 
    Gdx.gl.glClearColor(0, 0, 0, 1); 
} 

public void render(float delta) { 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    sb.begin(); 
     splash.draw(sb); 
    sb.end(); 
} 

public void resize(int width, int height) { } 
public void resume() { } 
public void dispose() { } 
public void hide() { } 
public void pause() { } 
} 

任何想法,什麼可能導致飛濺類不渲染圖像的問題?

更新1:我發現,那飛濺類中的渲染()方法,甚至不被調用(但show()方法一樣)

回答

1

你應該從你的遊戲類調用渲染方法。如果你不打電話,沒有人會爲你做。你不應該重寫你所做的遊戲類的渲染方法,而是將它做成空的,這樣它就不會爲你調用任何東西。

讓我們來看看遊戲類。它確實實現了ApplicationListener接口中的所有方法(不是create())。所以你不需要重寫它的任何內容。只需刪除您的:

public void render() { } 
public void dispose() { super.dispose(); } 
public void pause() { super.pause(); } 
public void resize(int width, int height) { super.resize(width, height); } 
public void resume() { } 

它應該可以正常工作。儘管這些都是無用的方法。他們沒有什麼比調用超類更好,所以你爲什麼寫這些。如果你沒有寫出它會自動調用超級方法。

但是,如果你想自己處理這些東西,比如在屏幕上調用dispose或者在設置新屏幕之前調用init,你需要編寫自己的「遊戲」類來實現ApplicationListener接口。

爲了給你如何做到這一點生病後,我用了一些測試一個小例子一個想法:

public class MyGameClass implements ApplicationListener { 

    private Screen curScreen; // the current screen 

    @Override 
    public void create(){ 
     Gdx.graphics.getWidth(); 
     Gdx.graphics.getHeight(); 

     Intro temp = new Intro(this);//just an example of the first screen to load up 
     setScreen(temp); 
    } 

    public void setScreen(Screen s) { 
     if (curScreen != null) { 
      curScreen.hide(); 
      curScreen.dispose(); 
     } 
     curScreen = s; 
     curScreen.show(); 
     curScreen.resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 
     s.init(); 
    } 

    @Override 
    public void dispose() { 
     curScreen.dispose(); 
    } 

    @Override 
    public void render() { 
     curScreen.render(Gdx.graphics.getDeltaTime()); //call the rendermethod with the delta time as parameter 
    } 

    @Override 
    public void resize(int width, int height) { 
     curScreen.resize(width, height); 
    } 

    @Override 
    public void pause() { 
     curScreen.pause(); 
    } 

    @Override 
    public void resume() { 
     curScreen.resume(); 
    } 
} 
+1

謝謝你的提示,但我已經想通了。我沒有在主類中調用'super.render()'。所以把它放在那裏解決了它。 無論如何,你的解決方案似乎也能工作。 – ViliX64