Java 渲染字体后fps突然下降

Java 渲染字体后fps突然下降,java,performance,libgdx,frame-rate,Java,Performance,Libgdx,Frame Rate,所以我正在做一个简单的侧滚游戏,比如flappy bird,当我添加音乐和所有精灵时,游戏运行得很顺利。但是当我添加字体(ttf)时,游戏的fps突然下降,并且落后了很多。 需要帮忙吗 以下是我的渲染方法: @Override public void render(SpriteBatch sb) { sb.setProjectionMatrix(cam.combined); sb.begin(); sb.draw(actualGamebg, cam.position.

所以我正在做一个简单的侧滚游戏,比如flappy bird,当我添加音乐和所有精灵时,游戏运行得很顺利。但是当我添加字体(ttf)时,游戏的fps突然下降,并且落后了很多。 需要帮忙吗

以下是我的渲染方法:

 @Override
public void render(SpriteBatch sb) {

    sb.setProjectionMatrix(cam.combined);
    sb.begin();
    sb.draw(actualGamebg, cam.position.x - (cam.viewportWidth/2), 0);
    sb.draw(bird.getTexture(), bird.getPosition().x , bird.getPosition().y);
    for(Tube tube: tubes) {

        sb.draw(tube.getTopTube(), tube.getPosTopTube().x, tube.getPosTopTube().y);
        sb.draw(tube.getBottomTube(), tube.getPosBottomTube().x, tube.getPosBottomTube().y);
    }
    sb.draw(ground,groundPos1.x,groundPos1.y);
    sb.draw(ground,groundPos2.x,groundPos2.y);

    font24.draw(sb,SCORE,24,100);
    sb.end();

}
我的完整游戏状态课程是:

package com.maharshmangal.game.State;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;
import com.maharshmangal.game.FlappyDemo;
import com.maharshmangal.game.Sprites.Bird;
import com.maharshmangal.game.Sprites.Tube;

import java.util.Timer;

/**
 * Created by Kronos on 28-12-2016.
 */

public class PlayState extends State {
    private static final int TUBE_SPACING = 100;
    private static final int TUBE_COUNT = 4;


    private Bird bird;
    private Texture actualGamebg;
    private Tube tube ;
    private Texture ground;
    private Vector2 groundPos1,groundPos2;
    private static final int HIGHEST_GROUND_LIMIT = -30;
    private Array<Tube> tubes;
    private int k;
    long startTime=0;
    private Music mainMusic;
    private Music scoreIncrease;
    private Music wingFlap;
    public BitmapFont font24;
    public String SCORE;






    public PlayState(GameStateManager gsm) {
        super(gsm);
        bird = new Bird(0,300);
        actualGamebg = new Texture("bg.png");
        cam.setToOrtho(false, FlappyDemo.WIDTH/2,FlappyDemo.HEIGHT/2);

        tubes =new Array<Tube>();
        ground = new Texture("ground.png");
        mainMusic = Gdx.audio.newMusic(Gdx.files.internal("mainmusic.mp3"));
        scoreIncrease = Gdx.audio.newMusic(Gdx.files.internal("smw_coin.ogg"));
        wingFlap = Gdx.audio.newMusic(Gdx.files.internal("sfx_wing.ogg"));

        font24= new BitmapFont();
        SCORE = new String();

        groundPos1 = new Vector2(cam.position.x -cam.viewportWidth/2, HIGHEST_GROUND_LIMIT);
        groundPos2 = new Vector2((cam.position.x - cam.viewportWidth/2) + ground.getWidth(),HIGHEST_GROUND_LIMIT);
        startTime = TimeUtils.nanoTime();

        for(int i=1 ; i<=TUBE_COUNT; i++)
        {

            tubes.add(new Tube(i* (TUBE_SPACING + Tube.TUBE_WIDTH)));
        }
        mainMusic.play();
        mainMusic.setVolume(0.8f);

    }

    @Override
    protected void handleInput() {
        if (Gdx.input.justTouched())
        bird.jump();
        wingFlap.setLooping(false);
        wingFlap.play();
        wingFlap.setVolume(0.1f);
    }




    @Override
    public void update(float dt) {
        handleInput();

         updateGround();

         bird.update(dt);
        if (TimeUtils.timeSinceNanos(startTime) > 1500000000)
        {
            Score();
            startTime = TimeUtils.nanoTime();
        }


        fontGenerator();
        SCORE = String.valueOf(k);





        for(int i =0 ; i< tubes.size;i++)
        {
            Tube tube= tubes.get(i);

            if (cam.position.x - (cam.viewportWidth/2) > tube.getPosTopTube().x + tube.getTopTube().getWidth())
            {
                tube.reposition(tube.getPosTopTube().x + ((Tube.TUBE_WIDTH + TUBE_SPACING) *TUBE_COUNT));
            }
            if(tube.collides(bird.getBounds()))
            {
                cam.position.x = bird.getPosition().x;
                mainMusic.stop();
                gsm.set(new GameOverState(gsm));
                          }

            else
                cam.position.x = bird.getPosition().x +80;

        }
        if (bird.getPosition().y <= ground.getHeight()){
            gsm.set(new GameOverState(gsm));
        }



        cam.update();

    }



    @Override
    public void render(SpriteBatch sb) {

        sb.setProjectionMatrix(cam.combined);
        sb.begin();
        sb.draw(actualGamebg, cam.position.x - (cam.viewportWidth/2), 0);
        sb.draw(bird.getTexture(), bird.getPosition().x , bird.getPosition().y);
        for(Tube tube: tubes) {

            sb.draw(tube.getTopTube(), tube.getPosTopTube().x, tube.getPosTopTube().y);
            sb.draw(tube.getBottomTube(), tube.getPosBottomTube().x, tube.getPosBottomTube().y);
        }
        sb.draw(ground,groundPos1.x,groundPos1.y);
        sb.draw(ground,groundPos2.x,groundPos2.y);

        font24.draw(sb,SCORE,24,100);
        sb.end();

    }

    /**
     * spritebatches must be drawn in order .The one at the bottommost acts as the top layer.
     */

    @Override
    public void dispose() {
        actualGamebg.dispose();
        bird.dispose();
        font24.dispose();
        for(Tube tube: tubes)
        {
            tube.dispose();
        }
        ground.dispose();


        System.out.println("Play State Disposed");

    }




  private void updateGround()
    {
        if (cam.position.x-(cam.viewportWidth/2) > groundPos1.x + ground.getWidth())
        {
            groundPos1.add(ground.getWidth()*2,0);
        }
        if (cam.position.x-(cam.viewportWidth/2) > groundPos2.x + ground.getWidth())
        {
            groundPos2.add(ground.getWidth()*2,0);
        }
    }


    public void Score()
    {
        k++;
        scoreIncrease.play();
        scoreIncrease.setVolume(0.3f);

    }


    public void fontGenerator(){
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("bitmapfont/PressStart2P.ttf"));
        FreeTypeFontGenerator.FreeTypeFontParameter parameter= new FreeTypeFontGenerator.FreeTypeFontParameter();

        parameter.size=24;
        parameter.color= Color.GOLD;
        parameter.borderColor= Color.GOLDENROD;
        font24= generator.generateFont(parameter);
    }

    public int getK(){return k;}
}
package com.maharshmangal.game.State;
导入com.badlogic.gdx.gdx;
导入com.badlogic.gdx.audio.Music;
导入com.badlogic.gdx.graphics.Color;
导入com.badlogic.gdx.graphics.Texture;
导入com.badlogic.gdx.graphics.g2d.Batch;
导入com.badlogic.gdx.graphics.g2d.BitmapFont;
导入com.badlogic.gdx.graphics.g2d.SpriteBatch;
导入com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
导入com.badlogic.gdx.math.Vector2;
导入com.badlogic.gdx.utils.Array;
导入com.badlogic.gdx.utils.TimeUtils;
导入com.maharshmangal.game.FlappyDemo;
导入com.maharshmangal.game.Sprites.Bird;
导入com.maharshmangal.game.Sprites.Tube;
导入java.util.Timer;
/**
*由Kronos于2016年12月28日创建。
*/
公共类PlayState扩展了状态{
专用静态最终内管间距=100;
专用静态最终整数管计数=4;
私人雀鸟;
私有纹理模型;
私用试管;
私人纹理地面;
专用矢量2 groundPos1,groundPos2;
私人静态最终int最高地面极限=-30;
专用阵列管;
私人INTK;
长起始时间=0;
私人音乐;
私人音乐得分增加;
私人音乐;
公共位图字体24;
公共弦乐谱;
公共播放状态(GameStateManager gsm){
超级(gsm);
鸟=新鸟(0300);
actualGamebg=新纹理(“bg.png”);
凸轮设置为(假,FlappyDemo.宽度/2,FlappyDemo.高度/2);
管=新阵列();
地面=新纹理(“ground.png”);
mainMusic=Gdx.audio.newMusic(Gdx.files.internal(“mainMusic.mp3”);
scoreIncrease=Gdx.audio.newMusic(Gdx.files.internal(“smw_coin.ogg”);
wingFlap=Gdx.audio.newMusic(Gdx.files.internal(“sfx_wing.ogg”);
font24=新的位图字体();
分数=新字符串();
groundPos1=新矢量2(凸轮位置x-cam视口宽度/2,最高地面限制);
groundPos2=新矢量2((cam.position.x-cam.viewportWidth/2)+ground.getWidth(),最高地面限制);
startTime=TimeUtils.nanoTime();
对于(int i=1;i 150000000)
{
分数();
startTime=TimeUtils.nanoTime();
}
fontGenerator();
分数=字符串。值(k);
对于(int i=0;itube.getPosTopTube().x+tube.getTopTube().getWidth())
{
重新定位(tube.getPosTopTube().x+((tube.tube_宽度+tube_间距)*tube_计数));
}
if(tube.collides(bird.getBounds()))
{
cam.position.x=bird.getPosition().x;
主音乐。停止();
gsm.set(新游戏过度状态(gsm));
}
其他的
cam.position.x=bird.getPosition().x+80;
}
if(bird.getPosition().y groundPos1.x+ground.getWidth())
{
groundPos1.add(ground.getWidth()*2,0);
}
如果(cam.position.x-(cam.viewportWidth/2)>groundPos2.x+ground.getWidth())
{
groundPos2.add(ground.getWidth()*2,0);
}
}
公开无效分数()
{
k++;
增加分数。玩();
分数增加。设定音量(0.3f);
}
公共生成器(){
FreeTypeFontGenerator=新的FreeTypeFontGenerator(Gdx.files.internal(“bitmapfont/PressStart2P.ttf”);
FreeTypeFontGenerator.FreeTypeFontParameter参数=新的FreeTypeFontGenerator.FreeTypeFontParameter();
参数。大小=24;
参数.color=color.GOLD;
参数.borderColor=Color.GOLDENROD;
font24=发电机GENERATEFORT(参数);
}
public int getK(){return k;}
}
如果你需要更多信息,一定要告诉我,谢谢。
干杯

您正在读取ttf文件并每帧创建一个新的BitmapFont对象。不要在update()中调用fontGenerator()。在构造器中调用一次。

谢谢,它成功了,游戏现在看起来好多了。如果你看到任何优化,你能建议代码中的其他优化吗?