简体   繁体   English

LibGdx Java中不显示文本

[英]Text not displaying in LibGdx Java

I am trying to add text for debugging purposes in my program. 我试图在程序中添加用于调试目的的文本。 I call the players debugDraw method like so in the main class: 我在主类中这样调用播放器的debugDraw方法:

public void create () {

    //Initialize essentials
    cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    cam.setToOrtho(false);

    rend = new ShapeRenderer();
    rend.setAutoShapeType(true);

    batch = new SpriteBatch();



    // Initialize Entities
    player = new Player(new Vector2(100, 100), new Vector2(100,100));
    enemy = new Enemy(new Vector2(100, 100), new Vector2(100,10));
}

@Override
public void render () {
    //Update player
    player.update();
    //Update camera then set matrix of batch
    cam.update();
    batch.setTransformMatrix(cam.combined);

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    // ShapeRenderer begin
    rend.begin();
    player.render(rend);
    enemy.render(rend);
    rend.end();
    // ShapeRenderer end

    // SpriteBatch begin
    batch.begin();
    player.renderDebugText(batch);
    batch.end();
    // SpriteBatch end

}

Here is the entity class which is the parent class to player: 这是实体类,它是玩家的父类:

 public Entity(Vector2 coords, Vector2 dims){
    //Assign constructors
    position = coords;
    dim = dims;

    //For debugging purposes of all entities
    debugText = new BitmapFont();
    debugText.setColor(Color.WHITE);
}


public void renderDebugText(SpriteBatch batch){
    debugText.draw(batch, "Vel.x: " + vel.x, 100, 100);

}

However when I run the program, I just get my normal screen with no text at all. 但是,当我运行该程序时,我的屏幕几乎没有任何文字。 I can't seem to figure out why this isn't working. 我似乎无法弄清楚为什么这行不通。 Any help is extremely appreciated. 任何帮助都非常感谢。

Nothing immediately looks wrong with the code you posted, so here's a few ideas; 您发布的代码不会立即看起来有问题,因此这里有一些建议;

The default BitmapFont is 15pt, if this is drawn at 15px height then it could be very small if you force your game to a high resolution, like Full-HD. 默认的BitmapFont是15pt,如果以15px的高度绘制,则如果将游戏强制为高分辨率(例如Full-HD),则可能会很小。 My current project is Full-HD and the font I use looks just about right at 45px, so you could try scaling yours by a factor of 3 or 4. Eg use this after initialising your font; 我当前的项目是Full-HD,我使用的字体大约为45px,因此您可以尝试将其缩放3或4倍。例如,在初始化字体后使用此字体。

bitmapFont.getData().setScale(3);

Your Camera should also have the virtual/viewport dimensions, so if you are forcing a particular resolution then you should pass in your virtual dimensions instead. 您的Camera还应具有虚拟/视口尺寸,因此,如果要强制使用特定分辨率,则应传递虚拟尺寸。

As @Tenfour04 has suggested, you should try to avoid multiple instances of the same font, so instead of initialising your font in the Entity class, initialise it in your main game and pass it through the Entity constructor. 正如@ Tenfour04所建议的那样,您应尽量避免使用同一字体的多个实例,因此,不要在Entity类中初始化字体,而应在主游戏中对其进行初始化并通过Entity构造函数进行传递。 I can't see how this would fix your issue though as this would be purely for performance. 我看不到如何解决您的问题,尽管这纯粹是为了提高性能。

I made a very simple mistake, but from how much code I posted, it is easily missed. 我犯了一个非常简单的错误,但是从发布的代码数量来看,很容易错过。 On the line where I put 在我放的线上

batch.setTransformMatrix(cam.combined);

it should be replaced with 它应该替换为

batch.setProjectionMatrix(cam.combined);

Then all errors go away, sorry, I don't know why it took me so long to figure out. 然后所有错误消失了,对不起,我不知道为什么我花了这么长时间才弄清楚。 Thanks for all the help! 感谢您的所有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM