1

我正在尝试在一个练习项目中加载一种字体,libgdx就像这样。

package peace.at.steamd.screens;

import peace.at.steamd.Steamd;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;

public class mainmenu implements Screen{
Steamd game;
Stage stage;
BitmapFont black;
TextureAtlas atlas;
Skin skin;
SpriteBatch batch;
TextButton button;
public mainmenu(Steamd game){
    this.game= game;

}

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

    batch.begin();
    stage.draw();
    batch.begin();

}

@Override
public void resize(int width, int height) {
    if(stage== null)
        stage= new Stage(width,height,true);
    stage.clear();
    Gdx.input.setInputProcessor(stage);
    TextButtonStyle style= new TextButtonStyle();
    style.up= skin.getDrawable("buttonnormal"); 
    style.down= skin.getDrawable("buttonpressed");
    style.font=black;
    button=new TextButton("Press Me",style);
    button.setWidth(400);
    button.setHeight(300);
    button.setX(100);
    button.setY(100);
    stage.addActor(button);
}

@Override
public void show() {
    batch=new SpriteBatch();
    atlas=new TextureAtlas("data/button.pack");
    skin= new Skin();
    skin.addRegions(atlas);
    black= new BitmapFont(Gdx.files.internal("data/custom.fnt"),false);
}

@Override
public void hide() {
    dispose();
}

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

}

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

}

@Override
public void dispose() {
    batch.dispose();
    skin.dispose();
    atlas.dispose();
    black.dispose();
    stage.dispose();

}

}

custom.fnt 位于正确的目录中并已链接等。但是,当我运行示例时,出现此错误。

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Error loading font file: data/custom.fnt
    at com.badlogic.gdx.graphics.g2d.BitmapFont$BitmapFontData.<init>(BitmapFont.java:823)
    at com.badlogic.gdx.graphics.g2d.BitmapFont.<init>(BitmapFont.java:99)
    at peace.at.steamd.screens.mainmenu.show(mainmenu.java:65)
    at com.badlogic.gdx.Game.setScreen(Game.java:62)
    at peace.at.steamd.screens.SplashScreen.tweencompleted(SplashScreen.java:82)
    at peace.at.steamd.screens.SplashScreen.access$0(SplashScreen.java:80)
    at peace.at.steamd.screens.SplashScreen$1.onEvent(SplashScreen.java:74)
    at aurelienribon.tweenengine.BaseTween.callCallback(BaseTween.java:380)
    at aurelienribon.tweenengine.BaseTween.updateStep(BaseTween.java:521)
    at aurelienribon.tweenengine.BaseTween.update(BaseTween.java:424)
    at aurelienribon.tweenengine.TweenManager.update(TweenManager.java:166)
    at peace.at.steamd.screens.SplashScreen.render(SplashScreen.java:42)
    at com.badlogic.gdx.Game.render(Game.java:46)
    at peace.at.steamd.Steamd.render(Steamd.java:23)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
    at com.badlogic.gdx.graphics.g2d.BitmapFont$BitmapFontData.<init>(BitmapFont.java:718)
    ... 15 more

当它到达该点时,它会尝试加载字体。我使用 hiero 网络启动器来创建字体,字体文件本身并没有什么特别之处。有任何想法吗?

4

0 回答 0