gpt4 book ai didi

java - Libgdx BitmapFont 帧率错误

转载 作者:太空宇宙 更新时间:2023-11-04 15:02:25 24 4
gpt4 key购买 nike

我有一个 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 并在您的创建方法中对其进行初始化 + 从 drawScore 方法中删除字体创建

关于java - Libgdx BitmapFont 帧率错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22426708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com