如何使字体文字可点击?

时间:2016-01-31 05:49:01

标签: java libgdx

我的游戏屏幕上有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();  
  }

1 个答案:

答案 0 :(得分:1)

您希望具有String,BitmapFont,Layout和position的组合功能。最好的方法是为这个可点击的字体创建一个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行,不包括注释。可以例外,但任何大型方法都可以分解为更易读的小方法。

现在如何使用这个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();  
  }