LibGDX Android游戏-在滚动屏幕上显示固定文本(即分数) [英] LibGDX Android Game - displaying fixed text (ie Score) over a scrolling screen

查看:20
本文介绍了LibGDX Android游戏-在滚动屏幕上显示固定文本(即分数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我在屏幕的右下角有两个叠加的纹理,一个左箭头和一个右箭头,它们被用作操纵板来控制角色。我将它们与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等)放到一个舞台中,但我无法理解如何使它与我现有的代码一起工作。

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

和我的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等相关的事情的工作方式,但似乎无法使其按我的要求工作。

有没有人能给我提个建议,让舞台和演员工作,或者安装第二台摄像机之类的东西,来帮助我在角落里实现固定的分数展示。

我需要使用场景2D还是什么--啊哈!我的头要爆炸了……

我期待并提前感谢您。

问候

詹姆斯

推荐答案

我有几个建议:

    检查是否已将setUseIntegerPositions设置为True (默认设置)。如果你这样做了,而且你正在扩大 它,那么它可能会引起一些奇怪的效果,类似于你 描述一下。将其设置为False,然后查看它是否解决了问题。

  1. 在绘制文本之前重置SpriteBatch的矩阵,这样您就不需要针对滚动进行调整。

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

这篇关于LibGDX Android游戏-在滚动屏幕上显示固定文本(即分数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆