2017-01-14 4 views
0

私はこのフラットな鳥のような単純なサイドスクロールゲームで作業しており、音楽とすべてのスプライトを追加するとスムーズに動いていました。しかしフォント(ttf)ゲームはfpsの急激な低下を遂げました。 助けてください?ここでフォントをレンダリングした後のサスペンダーのドロップ数

は私のrenderメソッドです:

@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

これはうまくいきました。ゲームは今よりずっと良くなっています。コードがあれば、他の最適化を提案できますか? –

関連する問題