简体   繁体   中英

Java libGdx: FreeTypeFontGenerator is not working (Black screen)

I added in a custom font Aller_light.ttf and when I try to run the program with the font drawn with no errors the screen is black, so I'm not sure if its rendering somewhere else off screen or its not rendering at all.
If you could give your 2 cents that would be of great value to me.

Now it's saying i have a nullPointerExeption 现在说我有一个nullPointerExeption

Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.mygdx.finis.states.MenuState.draw(MenuState.java:43)
    at com.mygdx.finis.states.GameStateManager.draw(GameStateManager.java:31)
    at com.mygdx.finis.Main.render(Main.java:36)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)

This is what should be happening:

MenuState Class

package com.mygdx.finis.states;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.mygdx.finis.Assets;
import com.mygdx.finis.Main;

public class MenuState extends GameState{

    private SpriteBatch sb;

    private BitmapFont font;

    private int currentItem;
    private String[] menuItems;

    protected MenuState(GameStateManager gsm) {
        super(gsm);
    }

    public void init() {

        sb = new SpriteBatch();

        FreeTypeFontGenerator gen = new FreeTypeFontGenerator(
            Gdx.files.internal(null));

        font = gen.generateFont(20);

        menuItems = new String[]{
            "Play"
        };
    }

    public void update(float dt) {
        handleInput();
    }

    public void draw() {
        sb.setProjectionMatrix(Main.cam.combined);//*** This is where the null pointer starts *********

        sb.begin();
            font.draw(sb, "Quack", Main.WIDTH / 2, Main.HEIGHT / 2);//*** This does not work *******************
        sb.end();

    }

    public void handleInput() {

    }

    public void dispose() {

    }

}

Main Class

package com.mygdx.finis;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.mygdx.finis.states.GameStateManager;

public class Main implements ApplicationListener{

    public static int WIDTH;
    public static int HEIGHT;

    public static OrthographicCamera cam;

    private GameStateManager gsm;

    public void create(){
        WIDTH = Gdx.graphics.getWidth();
        HEIGHT = Gdx.graphics.getHeight();

        cam = new OrthographicCamera(WIDTH, HEIGHT);
        cam.translate(WIDTH / 2, HEIGHT / 2);
        cam.update();

        gsm = new GameStateManager();
    }

    public void render(){
        Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
        Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

        gsm.update(Gdx.graphics.getDeltaTime());
        gsm.draw();//****** THIS IS WHERE THE ISSUE ENDS ******
    }

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

It seems most likely that your SpriteBatch sb is null. Are you absolutely sure init() is being called?

The only other alternative is that Main.cam is null.

It has to be one of those.

Incidentally, this looks odd to me...

Gdx.files.internal(null));

Why are you doing that?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM