java - libgdx 我的启动画面没有显示

标签 java screen libgdx

我正在编写一个 libgdx 游戏,但似乎我无法显示我的启动画面,它只显示黑屏。有谁知道这是怎么来的?

我没有收到任何运行时错误,它只显示黑屏,即使我将 glClearColor 设置为其他颜色然后黑色

package mygame;



import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;  
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

 public class SplashScreen implements Screen {

Texture splashTexture;
Sprite splashSprite;
SpriteBatch batch;
MyGame game;

public SplashScreen(MyGame game) {
    this.game = game;
}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    batch.begin();
    splashSprite.draw(batch);
    batch.end();
}

@Override
public void resize(int width, int height) {

}

@Override
public void show() {
    splashTexture = new Texture("mygame/splash.png");
    splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    splashSprite = new Sprite(splashTexture);
    //splashSprite.setColor(1, 1, 1, 0);
    splashSprite.setX(Gdx.graphics.getWidth() / 2 - (splashSprite.getWidth() / 2));
    splashSprite.setY(Gdx.graphics.getHeight() / 2 - (splashSprite.getHeight() / 2));

    batch = new SpriteBatch();
}

@Override
public void hide() {

}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void dispose() {

}

}

最佳答案

我尝试了您的代码(见下文),它运行得很好,所以问题可能出在 MyGame 类中。例如,您是否重写了 Game.render(),然后不调用 super.render()?

package hacks;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class MyGame extends com.badlogic.gdx.Game {

    @Override
    public void create() {
        setScreen(new SplashScreen(this));
    }

    public static void main(String[] args) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();

        config.title = MyGame.class.getName();
        config.width = 800;
        config.height = 480;
        config.fullscreen = false;
        config.useGL20 = true;
        config.useCPUSynch = true;
        config.forceExit = true;
        config.vSyncEnabled = true;

        new LwjglApplication(new MyGame(), config);
    }
}

class SplashScreen implements Screen {

    Texture splashTexture;
    Sprite splashSprite;
    SpriteBatch batch;
    MyGame game;

    public SplashScreen(MyGame game) {
        this.game = game;
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        batch.begin();
        splashSprite.draw(batch);
        batch.end();
    }

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

    @Override
    public void show() {
        splashTexture = new Texture("assets/data/libgdx.png");
        splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

        splashSprite = new Sprite(splashTexture);
        // splashSprite.setColor(1, 1, 1, 0);
        splashSprite.setX(Gdx.graphics.getWidth() / 2 - (splashSprite.getWidth() / 2));
        splashSprite.setY(Gdx.graphics.getHeight() / 2 - (splashSprite.getHeight() / 2));

        batch = new SpriteBatch();
    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {
    }
}

关于java - libgdx 我的启动画面没有显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15559021/

相关文章:

java - 无法将Object []转换为Integer []

ios - 在 MonoDeveloper 中获取 java.net.MalformedURLException

java - 将值从数组转换为 findViewById id

python - 使用 PIL 或 cv2 等模块在 python 中捕获屏幕的最有效方法是什么?因为它占用了很多内存

unity3d - 具有不同屏幕方向的 Unity 应用程序

Android 在应用程序中保持屏幕开启

java - LibGDX 第一次加载 scene2D 小部件非常慢,需要建议

intellij-idea - 使用 Gradle 导入时运行桌面 libgdx 时出错

java - 即使我们目前不打算迁移到 Java 8+,我们也可以升级到 Spring 5 吗?

java - 如何使用 JavaScript 处理 Java 对象序列化?