2014-05-04 35 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