libgdx AssetManager preload ttf字体不起作用

时间:2017-08-19 04:44:38

标签: libgdx kotlin

我在应用程序启动时执行了此代码

val resolver = InternalFileHandleResolver()
asset.setLoader(FreeTypeFontGenerator::class.java, FreeTypeFontGeneratorLoader(resolver))
asset.setLoader(BitmapFont::class.java, ".ttf", FreetypeFontLoader(resolver))

val menuFont = FreeTypeFontLoaderParameter()
menuFont.fontFileName = FONT_FN
menuFont.fontParameters.size = 10
asset.load(FONT_MENU, BitmapFont::class.java, menuFont)

val gameFont = FreeTypeFontLoaderParameter()
gameFont.fontFileName = FONT_FN
gameFont.fontParameters.size = 15
asset.load(FONT_GAME, BitmapFont::class.java, gameFont)

asset.finishLoading()

asset是我的AssetManager,常量是:

const val FONT_FN = "others/font.ttf"
const val FONT_MENU = "FONT_MENU"
const val FONT_GAME = "FONT_GAME"

现在我有了这些辅助函数:

private fun addLabel(text: String, x: Float, y: Float, font: BitmapFont, color: Color, stage: Stage): Label {
    val label = Label(text, Label.LabelStyle(font, color))
    label.setPosition(x, y)
    stage.addActor(label)
    return label
}

fun addLabelForMenu(text: String, x: Float, y: Float, stage: Stage): Label {
     val font = asset.get(FONT_MENU, BitmapFont::class.java)
     val color = Color.WHITE
     return addLabel(text, x, y, font, color, stage)
}

fun addLabelForGame(text: String, x: Float, y: Float, stage: Stage): Label {
     val font = asset.get(FONT_GAME, BitmapFont::class.java)
     val color = Color.WHITE
     return addLabel(text, x, y, font, color, stage)
}

调用addLabelForMenu时游戏崩溃:

com.badlogic.gdx.utils.GdxRuntimeException: Asset not loaded: others/font.ttf

我在这里关注维基页面:https://github.com/libgdx/libgdx/wiki/Managing-your-assets

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

const val FONT_FN = "others/font.ttf"    // Make sure your `font.ttf` file is inside `others` folder

.ttf扩展名添加到您用于加载和检索的fileName。

所以改变

const val FONT_MENU = "FONT_MENU"
const val FONT_GAME = "FONT_GAME"

const val FONT_MENU = "FONT_MENU.ttf"
const val FONT_GAME = "FONT_GAME.ttf"