libgdx我的启动画面没有显示

时间:2013-03-21 21:59:35

标签: 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() {

}

}

3 个答案:

答案 0 :(得分:1)

我尝试了你的代码(见下文)并且它运行良好,所以问题可能在于 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() {
    }
}

答案 1 :(得分:0)

这就是我的游戏类的样子:

package mygame;

import com.badlogic.gdx.Game;

 public class MyGame extends Game {

@Override
public void create() {

    setScreen(new SplashScreen(this));
}

@Override
public void resize(int width, int height) {
    // TODO Auto-generated method stub

}

@Override
public void render() {
    // TODO Auto-generated method stub

}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public void resume() {
    // TODO Auto-generated method stub

}

@Override
public void dispose() {
    // TODO Auto-generated method stub

}

} 

答案 2 :(得分:0)

这就是你的游戏类应该是这样的......你需要调用超级函数。

package com.sample;

import com.badlogic.gdx.Game;

public class Sample extends Game{

    @Override
    public void dispose() {
        // TODO Auto-generated method stub
        super.dispose();
    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub
        super.pause();
    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub
        super.resume();
    }

    @Override
    public void render() {
        // TODO Auto-generated method stub
        super.render();
    }

    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub
        super.resize(width, height);
    }

    @Override
    public void create() {
        // TODO Auto-generated method stub
        setScreen(new AnotherScreen(this));
    }

}