我在应用程序启动时执行了此代码
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
可能是什么问题?
答案 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"