我有一个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);
}
最佳答案
您正在每帧创建字体,这就是为什么它很慢的原因:
BitmapFont bf = new BitmapFont(Gdx.files.internal("data/droidserif.fnt"), Gdx.files.internal("data/droidserif.png"),false);
==>在Game类
BitmapFont bf
中创建一个字段,并在create方法中对其进行初始化+从drawScore方法中删除字体创建