首页 > 解决方案 > LibGDX:根据单词的长度在屏幕上创建文本按钮的数量?

问题描述

我正在考虑一种类似 4 Pics 1 Word-like 的游戏回答方法,其中有一个空白并显示一组字符供玩家单击以输入可能的答案。我只是在想如何使显示的文本按钮的数量与单词的字符数量(单词的长度)相同?

标签: javalibgdxgame-development

解决方案


与获取宽度有关

// for BitmapFont API < 1.5.6
float width = font.getBounds(yourWord).width;
float edgeWidth = 5f; // indention 
yourTextButton.setWidth(width + 2 * edgeWidth);

// for BitmapFont API >= 1.5.6
GlyphLayout layout = new GlyphLayout();
layout.setText(yourWord);
float width = layout.width;
float edgeWidth = 5f; // indention 
yourTextButton.setWidth(width + 2 * edgeWidth);

您也可以使用Table来完成任务,它应该自己计算大小

Table table = new Table();
TextButton textButton = new TextButton(yourWord, skin);
table.add(textButton);

推荐阅读