有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    您希望拥有字符串、位图字体、布局和定位的组合功能。最好的方法是为这个可点击的字体创建一个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();  
      }