Libgdx BitmapFont坏帧率

时间:2014-03-15 16:46:24

标签: java libgdx frame-rate bitmap-fonts

我有一个BitmapFont来显示玩家得分。玩家以恒定的速度移动。 对于BitmapFont,我使用第二个OrtographicCamera和SpriteBatch,所以我不需要重新计算字体位置。

我的问题是:只要我包含BitmapFont,帧速率就会下降,整个游戏就会变得迟钝。我做错了什么?

public void render(float delta) {

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

    rbg.render(delta);

    spriteBatch.setProjectionMatrix(cam.combined);
    spriteBatch.begin();    
        drawBlocks();
        drawBob();  
    spriteBatch.end();

    scoreBatch.setProjectionMatrix(scoreCam.combined);
    scoreBatch.begin();
        drawScore(this.score);
    scoreBatch.end();

    if (debug)
        drawDebug();
}

    private void drawBob() {
    bobFrame = bob.isFacingLeft() ? bobIdleLeft : bobIdleRight;
    if(bob.getState().equals(State.WALKING)) {
        bobFrame = bob.isFacingLeft() ? walkLeftAnimation.getKeyFrame(bob.getStateTime(), true) : walkRightAnimation.getKeyFrame(bob.getStateTime(), true);
    } else if (bob.getState().equals(State.JUMPING)) {
        if (bob.getVelocity().y > 0) {
            bobFrame = bob.isFacingLeft() ? bobJumpLeft : bobJumpRight;
        } else {
            bobFrame = bob.isFacingLeft() ? bobFallLeft : bobFallRight;
        }
    }
    spriteBatch.draw(bobFrame, bob.getPosition().x * ppuX, bob.getPosition().y * ppuY, Bob.WIDTH * ppuX, Bob.HEIGHT * ppuY);

    cam.position.set(cam.position.x + 24f, Gdx.graphics.getHeight() / 2f, 0);
    cam.update(); } 
private void drawBlocks() {
    for (Block block : world.getDrawableBlocks((int)CAMERA_WIDTH, (int)CAMERA_HEIGHT)) {
        spriteBatch.draw(blockTexture, block.getPosition().x * ppuX, block.getPosition().y * ppuY, Block.SIZE * ppuX, Block.SIZE * ppuY);
    }
    for (Block block : world.getBottomBlocks((int)CAMERA_WIDTH, (int)CAMERA_HEIGHT)) {
        spriteBatch.draw(blockBottomTexture, block.getPosition().x * ppuX, block.getPosition().y * ppuY, Block.SIZE * ppuX, Block.SIZE * ppuY);
    }
    for (Block block : world.getTopBlocks((int)CAMERA_WIDTH, (int)CAMERA_HEIGHT)) {
        spriteBatch.draw(blockTopTexture, block.getPosition().x * ppuX, block.getPosition().y * ppuY, Block.SIZE * ppuX, Block.SIZE * ppuY);
    }
}
public void drawScore(String score) {
    BitmapFont bf = new BitmapFont(Gdx.files.internal("data/droidserif.fnt"), Gdx.files.internal("data/droidserif.png"),false);
    bf.setUseIntegerPositions(false);
    bf.setScale(2);
    bf.draw(scoreBatch, this.score, 20,50); 
}

1 个答案:

答案 0 :(得分:0)

您正在创建每个框架的字体,这就是它很慢的原因:

BitmapFont bf = new BitmapFont(Gdx.files.internal("data/droidserif.fnt"), Gdx.files.internal("data/droidserif.png"),false);

==>在你的游戏类BitmapFont bf中创建一个字段并在你的create方法中初始化它+从drawScore方法中删除字体创建