所以我在这个简单的侧滚动游戏中工作,比如飞扬的鸟,当我添加音乐和所有精灵时,游戏运行顺畅。但是当我添加字体(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();
}
我的完整的Playstate课程是:
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;}
}
如果您需要更多信息和谢谢,请告诉我。 干杯
答案 0 :(得分:0)
您正在读取ttf文件并在每帧中创建一个新的BitmapFont对象。不要在update()中调用fontGenerator()。在构造函数中调用一次。