gpt4 book ai didi

java - 尝试从空对象引用上的字段 'GlyphRun.color' 读取

转载 作者:行者123 更新时间:2023-12-01 08:50:33 27 4
gpt4 key购买 nike

我一直在努力让字体与 libgdx 一起使用,我终于做到了。

但现在它抛出了一个异常,我无法弄清楚。

java.lang.NullPointerException: Attempt to read from field 'com.badlogic.gdx.graphics.Color com.badlogic.gdx.graphics.g2d.GlyphLayout$GlyphRun.color' on a null object reference
at com.badlogic.gdx.graphics.g2d.GlyphLayout.setText(GlyphLayout.java:141)
at com.badlogic.gdx.graphics.g2d.BitmapFontCache.addText(BitmapFontCache.java:505)
at com.badlogic.gdx.graphics.g2d.BitmapFontCache.addText(BitmapFontCache.java:487)
at com.badlogic.gdx.graphics.g2d.BitmapFontCache.setText(BitmapFontCache.java:438)
at yt.agar.game.Screens.GameScreen.render(GameScreen.java:213)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:459)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1522)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1239)

GlyphLayout.java:141: https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/g2d/GlyphLayout.java#L141

我这样绘制字体:

BitmapFont font = assetManager.get('arial-96.otf');
font.draw(spriteBatch, "sample text", 10, 10);

我使用AssetManager以这种方式生成位图字体。我尝试过很多不同的字体,包括 truetype 字体。

FreetypeFontLoader.FreeTypeFontLoaderParameter font = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
font.fontFileName = "fonts/arial.otf";
font.fontParameters.color = Color.WHITE;
font.fontParameters.borderColor = Color.BLACK;
font.fontParameters.borderWidth = 3;
font.fontParameters.flip = true;
font.fontParameters.characters = FreeTypeFontGenerator.DEFAULT_CHARS;
font.fontParameters.size = 96;

manager.load("arial-96.otf", BitmapFont.class, font);

我看不出我做错了什么。

最佳答案

我尝试过这种方式,它对我来说效果很好:

public class TestGame extends Game {

private AssetManager assetManager;
private SpriteBatch spriteBatch;

@Override
public void create() {

assetManager=new AssetManager();
spriteBatch=new SpriteBatch();
FileHandleResolver resolver = new InternalFileHandleResolver();
assetManager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
assetManager.setLoader(BitmapFont.class, ".otf", new FreetypeFontLoader(resolver));
FreetypeFontLoader.FreeTypeFontLoaderParameter size03Params = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
size03Params.fontFileName = "CHARLEMAGNESTD-BOLD.otf";
size03Params.fontParameters.size = 96;
assetManager.load("CHARLEMAGNESTD-BOLD.otf", BitmapFont.class, size03Params);

assetManager.finishLoading();
}

@Override
public void render() {

spriteBatch.begin();

if(assetManager.update()){
BitmapFont font=assetManager.get("CHARLEMAGNESTD-BOLD.otf");
font.draw(spriteBatch,"HEOOOW",100,100);
}

spriteBatch.end();
}

@Override
public void dispose() {
spriteBatch.dispose();
assetManager.dispose();
}
}

关于java - 尝试从空对象引用上的字段 'GlyphRun.color' 读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42426291/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com