java如何使字体文本可点击?
我的游戏屏幕上有3BitmapFont
(稍后更多)。我希望能够触摸字体并在控制台中输出它的字符串,这样我就知道按下了哪个。我试图创建一个矩形,但无法获取被触摸的BitmapFont
的字符串
以下是我创建BitmapFont
的代码:
public class simple implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
BitmapFont font;
GlyphLayout layout;
String a1 = "aa";
String a2 = "bb";
String a3 = "cc";
int a = 0;
@Override
public void create() {
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
batch = new SpriteBatch();
layout = new GlyphLayout();
font = new BitmapFont(Gdx.files.internal("arial-15.fnt"));
}
@Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
for (int i =1; i< 4;i++){
layout.setText(font, "a"+i);
font.draw(batch, layout,200+(15*i),200 );
}
batch.end();
}
# 1 楼答案
您希望拥有字符串、位图字体、布局和定位的组合功能。最好的方法是为这个可点击的字体创建一个
class
,它包含我们需要的所有内容。我为你做了一些工作,因为我是一个好人,实际上我还有很多事情要做:D正如你所见,它会分步解决你的问题。解决问题就是你在编程中所做的一切。经验法则是,一个方法的行数不能超过10行,注释除外。可以做出例外,但任何大型方法都可以分解为可读性更高的小型方法
现在如何使用这个ClickableFont类