1

我有一个 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); 
}
4

1 回答 1

0

您正在帧创建字体,这就是为什么它很慢:

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

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

于 2014-03-15T17:11:48.210 回答