LibGDX Android Game - displaying fixed text (ie Score) over a scrolling screen

烂漫一生 提交于 2019-12-24 18:09:02

问题


I am beginning to write a game in LibGDX, only just beginning. I have got a basic tile map loaded, a player sprite and can move the character around and the screen (camera) scrolls around - perfect.

I have two overlayed textures in the bottom right of the screen, a left and right arrow, which are used as the joypad to control the character. I position these in relation to the players.x position, which is always fixed to the centre of the screen. Cool so far .... As below:

/////////////////////////////////////////////////////////////////
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);
}

I am now trying to put the player's score in the top left of the screen. The score is made of a Bitmap font, as below.

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

In my render() method I cam calling some other methods to update, like the positions of the leftJoypad etc, as in renderJoypad(). I am also calling my draw font method to update the position of the score, however, when I scroll it is all jerky, and sometimes it shows less characters than there should be.

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

I believe that I need to place the score (and any other on screen texts, HUD etc) into a stage, but I cannot understand how to get it working with my existing code.

My show method is as follows:

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

and my render() method is as follows:

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

}

I have tried to change the way things work with relation to the Spritebatch etc and just cannot seem to get it working as I require.

Can anyone suggest how I may approach getting a stage and actors to work, or a second camera or something to help me achieve a fixed score display in the corner.

Do I need to use Scene 2D or something - aahhh! My head is exploding....

I look forward and thank you in advance.

Regards

James


回答1:


I have a couple of suggestions:

  1. Check to see if you have setUseIntegerPositions set to true (the default) when you draw your font. If you do, and you're scaling it, then it can cause some odd effects similar to those that you describe. Set it to false and see if it fixes the problem.

  2. Reset your spritebatch's matrices before drawing the text, that way you won't need to adjust it for the scrolling.

I'd even go as far as to recommend not scaling the font if you can help it, because fonts often look a little odd after being scaled.



来源:https://stackoverflow.com/questions/19450627/libgdx-android-game-displaying-fixed-text-ie-score-over-a-scrolling-screen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!