【问题标题】:How to make font text clickable?如何使字体文本可点击?
【发布时间】:2016-01-31 05:49:00
【问题描述】:

我的游戏在屏幕上有 3 个BitmapFont(稍后会更多)。我希望能够触摸字体并在控制台中输出它的字符串,这样我就知道按下了哪个。我试图创建一个矩形,但我无法获得被触摸的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();  
  }

【问题讨论】:

  • 感谢编辑帮助
  • 您通过stackoverflow.com/questions/34996693/… 获得答案。
  • 我想为字符串 a1 获取一些按钮; a2;每个 a3;然后当用户单击它时,它应该显示字符串值。就像我按下 bitmapfont a1 时它在控制台中显示“aa”;还有一个问题是它没有绘制字符串 vlaue 它只是绘制 a1 而不是 "aa" 请给我写代码来完成它

标签: java libgdx


【解决方案1】:

您希望拥有 String、BitmapFont、Layout 和定位的组合功能。最好的方法是为这个包含我们需要的所有内容的可点击字体创建一个class。我为你做了一些工作,因为我是个好人,实际上我还有很多其他事情要做:D。

public class ClickableFont {

//Declare the fields
private GlyphLayout layout;
private BitmapFont font;

private String text;

private int posX;
private int posY;

/**
 * Constructs clickable text from a font
 * @param text Text to display
 * @param posX X position of the text
 * @param posY Y position of the text
 */
public ClickableFont(String text, int posX, int posY) {
    this.text = text;
    this.posX = posX;
    this.posY = posY;

    font = new BitmapFont(Gdx.files.internal("arial-15.fnt"));
    layout = new GlyphLayout(font, text);

}

/**
 * @param batch Draws the text using the given SpriteBatch.
 * @param camera Requires a camera to calculate touches between screen and world.
 */
public void update(SpriteBatch batch, OrthographicCamera camera)
{
    checkClicked(camera);

    font.draw(batch, layout, posX, posY);
}

/**
 * Checks if this object is clicked and outputs to console
 * @param camera the camera
 */
private void checkClicked(OrthographicCamera camera)
{
    if (Gdx.input.justTouched())
    {
        //Get screen coordinates
        Vector3 touch = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
        //Transform screen touch to world coordinates using the camera you are drawing with
        camera.unproject(touch);

        //System.out.println(getRectangle());
        //System.out.println(touch);

        if (getRectangle().contains(touch.x, touch.y))
        {
            System.out.println(text + " has been clicked.");
        }
    }
}

/**
 * Creates a rectangle for the sprite to perform collision calculations.
 * Since it seems font.draw draws from top to bottom (coordinate system of LibGDX is not consistent)
 * We have to adept the rectangle position Y position
 * @return rectangle of font bounds
 */
private Rectangle getRectangle()
{
    return new Rectangle(posX, posY - (int)layout.height, (int)layout.width, (int)layout.height);
}
}

如您所见,它分步解决您的问题。解决问题就是你在编程中所做的一切。一个经验法则是永远不要让一个方法超过 10 行,不包括 cmets。可以例外,但任何大方法都可以分解成更易读的小方法。

现在如何使用这个 ClickableFont 类?

    public class simple  implements ApplicationListener {
    private OrthographicCamera camera;
    private SpriteBatch batch;
    //folowing are not nececary anymore since it's handled by the new class
    //BitmapFont font; 
    //GlyphLayout layout; 
    //String a1 = "aa";
    //String a2 = "bb";
    //String a3 = "cc";

    int a = 0;

    //Declare a list to hold your clickable fonts
    List<ClickableFont> clickableFonts = new ArrayList<ClickableFont>();

    @Override
    public void create() {
      camera = new OrthographicCamera();
      camera.setToOrtho(false, 800, 480);
      batch = new SpriteBatch();

    //Add clickable fonts to the list
      clickableFonts.add(new ClickableFont("aa", 200, 200));
      clickableFonts.add(new ClickableFont("bb", 200 + 150, 200));
      clickableFonts.add(new ClickableFont("cc", 200 + 150 * 2, 200));
   }

   @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();
      /* replace your loop
      for (int i =1; i< 4;i++){ 
          layout.setText(font, "a"+i);
          font.draw(batch, layout,200+(15*i),200 );
      }*/
      for (ClickableFont font : clickableFonts)
      {
          font.update(batch, camera);
      }
      batch.end();  
  }

【讨论】:

  • 对不起,我再次阅读了您的问题,我明白我想念它了。我清理了你的问题,并会改变我的答案。
  • 我的问题是我有 40 个字符串;想画它们,如果有人触摸它;然后它在控制台中显示选定的文本。
  • @YanGodara 这就是班级正在做的事情。您将ClickableFont 对象添加到指定要显示和定位的文本的列表中。然后遍历列表并.update() 他们。在更新中,它会绘制文本并检查点击,并在点击后将文本输出到控制台。
  • 你能告诉我如何将它添加到舞台上吗?我通常创建一个“void draw 并在其中添加 (font.draw())”并在 render 中调用 stage.draw()。如何将所有这些文本添加到 stage。
  • A BitmapFont 不是演员,因此无法添加到舞台。如果你想在舞台上有一个可点击的字体,你应该首先问这个问题,因为你只需要一个 TextButton。你创建一个文本按钮,给它一个字体和文本,添加一个点击监听器并将它添加到舞台。
猜你喜欢
  • 2014-03-31
  • 1970-01-01
  • 2012-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多