LibGDX And​​roid 游戏 - 在滚动屏幕上显示固定文本(即分数)

标签 libgdx sprite

我开始在 LibGDX 中编写游戏,这才刚刚开始。我已经加载了一个基本的图 block map ,一个玩家 Sprite ,可以移动角色并且屏幕(相机)滚动 - 完美。

我在屏幕右下角有两个重叠的纹理,一个向左和向右的箭头,用作控制角色的 handle 。我将它们放置在与players.x位置相关的位置,该位置始终固定在屏幕的中心。到目前为止很酷......如下:

/////////////////////////////////////////////////////////////////
private void renderJoypad(float deltaTime)
{
    batch.draw(Assets.leftJoypad, blockman.position.x-14, 1, 3, 3);
    batch.draw(Assets.rightJoypad, blockman.position.x-9, 1, 3, 3);
}

我现在尝试将玩家的分数放在屏幕的左上角。乐谱由位图字体组成,如下所示。

font = new BitmapFont(Gdx.files.internal("fonts/minecrafter.fnt"),Gdx.files.internal("fonts/minecrafter.png"),false);
    font.setScale(0.02f);

在我的 render() 方法中,我调用了一些其他方法来更新,例如 leftJoypad 等,如 renderJoypad() 中。我还调用我的绘制字体方法来更新乐谱的位置,但是,当我滚动时,它很不稳定,有时它显示的字符少于应有的字符。

public void drawScore(float deltaTime) 
{
    font.draw(batch, "00000", blockman.position.x, 10);
}

我相信我需要将乐谱(以及屏幕上的任何其他文本、HUD 等)放入舞台中,但我无法理解如何让它与我现有的代码一起工作。

我的展示方法如下:

public void show()
{
    //load assets class to get images etc
    Assets Assets = new Assets();

    //start logging Framerate
    Gdx.app.log( GameScreen.LOG, "Creating game" );
    fpsLogger = new FPSLogger();

    //set the stage - bazinga

    Gdx.input.setInputProcessor(stage);

    //stage.setCamera(camera);
    //stage.setViewport(480, 360, false);

    batch = new SpriteBatch();

    //load the Joypad buttons 
    loadJoypad();

    //background image
    loadBackground();

    //sounds
    jumpSound = Gdx.audio.newSound(Gdx.files.internal("sounds/slime_jump.mp3"));
    //LOAD block man here

    // load the map, set the unit scale to 1/16 (1 unit == 16 pixels)
    loadMap();

    //draw the score
    font = new BitmapFont(Gdx.files.internal("fonts/minecrafter.fnt"),Gdx.files.internal("fonts/minecrafter.png"),false);
    font.setScale(0.02f);


    // create an orthographic camera, shows us 30x20 units of the world
    camera = new OrthographicCamera();
    camera.setToOrtho(false, 30, 20);
    camera.update();

    // create the Blockman we want to move around the world
    blockman = new Blockman();
    blockman.position.set(25, 8);
}

我的 render() 方法如下:

public void render(float delta)
{
    // get the delta time
    float deltaTime = Gdx.graphics.getDeltaTime();

    stage.act(deltaTime);
    stage.draw();

    // clear the screen
    Gdx.gl.glClearColor(0.3f, 0.3f, 0.3f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    renderBackground(deltaTime);

    batch = renderer.getSpriteBatch();

    //updateBlockman(deltaTime);
    blockman.update(deltaTime);

    // let the camera follow the blockMan, x-axis only
    camera.position.x = blockman.position.x;
    camera.update();

    // set the tile map rendere view based on what the
    // camera sees and render the map
    renderer.setView(camera);
    renderer.render();

    //start the main sprite batch
    batch.begin();


        // render the blockMan
        renderBlockman(deltaTime);
        renderJoypad(deltaTime);
        drawScore(deltaTime);

    batch.end();
    fpsLogger.log();

}

我试图改变与 Spritebatch 等相关的事物的工作方式,但似乎无法让它按照我的要求工作。

任何人都可以建议我如何让舞台和 Actor 工作,或者第二台摄像机或其他东西来帮助我在角落实现固定的分数显示。

我需要使用 Scene 2D 还是其他东西 - 啊啊!我的头要爆炸了......

我期待并提前感谢您。

问候

詹姆斯

最佳答案

我有几个建议:

  1. 检查您是否有 setUseIntegerPositions设置为真 (默认)当您绘制字体时。如果你这样做,并且你正在扩展 它,那么它可能会导致一些奇怪的影响,类似于你所看到的 描述。将其设置为 false 并查看是否可以解决问题。

  2. 在绘制文本之前重置 spritebatch 的矩阵,这样您就不需要为滚动而调整它。

如果可以的话,我什至建议不要缩放字体,因为缩放后字体通常看起来有点奇怪。

关于LibGDX And​​roid 游戏 - 在滚动屏幕上显示固定文本(即分数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19450627/

相关文章:

java - libgdx 使鼠标在几秒钟不活动后重新出现的问题

java - OpenGL 上下文 (libgdx)

java - libGDX 中的视频播放

python - 如何修复 pygame 中的 Sprite 动画?

android - SurfaceView 还是 GLSurfaceview?

java - 更改原点 (0,0) 和所有相对点的位置

java - 在 libgdx 中处理多点触控

Java Sprite 动画 - 似乎无法让我的 Sprite 正确重绘

android - 找不到不使用 GL11Ext 进行绘图的 android 2d opengl Sprite 类的合适示例

python - 在 Pygame 中识别群体中的单个 Sprite