在LibGDX中设置新屏幕
我有两个类别,一个是主要方法,另一个是飞溅。然而,在这两者之间切换并不起作用。我想在Splash类中绘制一个图像,但它立即变黑。当我将代码从splash移动到main类时,图像出现了 主要类别:在LibGDX中设置新屏幕,libgdx,rendering,splash-screen,Libgdx,Rendering,Splash Screen,我有两个类别,一个是主要方法,另一个是飞溅。然而,在这两者之间切换并不起作用。我想在Splash类中绘制一个图像,但它立即变黑。当我将代码从splash移动到main类时,图像出现了 主要类别: public class Main extends Game { public static int WIDTH, HEIGHT; public void create () { WIDTH = Gdx.graphics.getWidth(); HEIGHT = Gdx.graphic
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() { }
}
知道吗,是什么导致了在Splash类中不渲染图像的问题吗
更新1:我发现Splash类中的render()方法甚至没有被调用(但是show()方法被调用)您应该从游戏类中调用render方法。如果你不叫它,没有人会为你这么做。你不应该覆盖你所做的游戏类的render方法,你做的是空的,这样它就不会为你调用任何东西 让我们来看看游戏类。它确实从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,那么您需要编写自己的“game”类来实现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();
}
}
谢谢你的提示,但我已经弄明白了。我没有在主类中调用“super.render()”。所以把它放进去就解决了。不管怎样,你的解决方案似乎也能奏效。。