2017-01-14 1 views
0

So arbeite ich in diesem einfachen Side Scrolling-Spiel wie flappy Vogel und das Spiel lief reibungslos, wenn ich die Musik und alle Sprites hinzugefügt.Aber wenn ich font (ttf) hinzugefügt, um die Spiel hatte einen abrupten Rückgang der fps und es lag viel hinterher. Irgendwelche Hilfe bitte?Sudden Drop in fps nach dem Rendern einer Schriftart

Hier ist meine Render-Methode:

@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(); 

} 

Meine volle playstate Klasse:

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;} 
} 

Do lassen Sie mich wissen, wenn Sie weitere Informationen und Dank benötigen. Cheers

Antwort

0

Sie lesen die ttf-Datei und erstellen ein neues BitmapFont-Objekt für jeden Frame. Rufen Sie fontGenerator() nicht in update() auf. Nennen Sie es einmal im Konstruktor.

+0

danke es funktionierte und das Spiel sieht jetzt viel besser aus. Könnten Sie irgendeine andere Optimierung im Code vorschlagen, wenn Sie irgendwelche sehen? –

Verwandte Themen