2017-01-14 43 views
0

所以我在這個簡單的側面滾動遊戲中就像飛揚的小鳥一樣工作,當我添加音樂和所有sprites時,遊戲運行平穩。但是當我向它添加字體(ttf)遊戲的fps突然下降,並且落後了很多。 請幫忙嗎?渲染字體後突然下降到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

您正在閱讀ttf文件並每幀創建一個新的BitmapFont對象。不要在update()中調用fontGenerator()。在構造函數中調用一次。

+0

感謝它的工作,現在遊戲看起來好多了。如果你看到任何代碼,你可以在代碼中提出任何其他優化建議嗎? –