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

Jam*_*ern 1 sprite libgdx

我开始在LibGDX中开始玩游戏,才开始.我已经加载了一个基本的瓷砖地图,一个玩家精灵,可以移动角色,屏幕(相机)滚动 - 完美.

我在屏幕的右下角有两个重叠的纹理,一个左右箭头,用作控制角色的游戏手柄.我将这些与player.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);
}
Run Code Online (Sandbox Code Playgroud)

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

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

在我的render()方法中,我调用了一些其他方法来更新,比如leftJoypad等的位置,就像在renderJoypad()中一样.我也在调用我的绘制字体方法来更新乐谱的位置,但是,当我滚动它时,它都是生涩的,有时它显示的字符数量应该少于应有的数量.

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

我相信我需要将得分(以及任何其他屏幕文本,HUD等)放入舞台,但我无法理解如何使用我现有的代码.

我的show方法如下:

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);
}
Run Code Online (Sandbox Code Playgroud)

我的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();

}
Run Code Online (Sandbox Code Playgroud)

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

任何人都可以建议我如何接近一个舞台和演员工作,或第二个相机或东西,以帮助我在角落里实现固定的分数显示.

我是否需要使用场景2D或其他东西 - 啊!我的脑袋正在爆炸......

我期待并提前感谢你.

问候

詹姆士

Rod*_*yde 5

我有几点建议:

  1. 绘制字体时,检查是否将setUseIntegerPositions设置为true(默认值).如果你这样做,并且你正在缩放它,那么它可能会导致一些与你描述的类似的奇怪效果.将其设置为false,看看它是否解决了问题.

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

如果你能提供帮助,我甚至建议不要缩放字体,因为字体在缩放后通常看起来有些奇怪.