2015-12-15 98 views
1

我添加了一個自定義字體Aller_light.ttf,當我嘗試運行帶有沒有錯誤繪製的字體的程序時,屏幕是黑色的,所以我不確定是否其渲染在其他地方,或者根本不渲染。
如果你可以給你2美分,這對我很有價值。
Java libGdx:FreeTypeFontGenerator無法正常工作(黑屏)

編輯 現在,它說我有一個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類

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() { 

    } 

} 

主類

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(){} 
} 

回答

0

看起來你最有可能是SpriteBatch sb爲空。你確定init()被調用了嗎?

唯一的另一種選擇是Main.cam爲空。

它必須是其中之一。

順便說一句,這看起來很奇怪,我...

Gdx.files.internal(null)); 

爲什麼要那樣做?

+0

它不是空的,但之前是因爲我擺弄東西。真正的問題現在是nullpointerexeption –

+0

我如何確保調用init()? –

+0

這是你的代碼,所以你最好知道!但是在猜測中,可能在你在Main類的create()方法中實例化它之後。 –