2014-09-10 46 views
2

我已經使用LibGdx框架創建了一個Android應用程序。每當我將其閃光到我的Android設備時,它都能正常工作。但是,當我關閉應用程序並將其新鮮打開時,應用程序將顯示黑白矩形框而不是圖像。你能幫我解決這個問題嗎? enter image description here重新打開LibGdx Android APP不能正確繪製屏幕

代碼

package com.mygdx.game; 

import java.util.ArrayList; 

import com.badlogic.gdx.ApplicationListener; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.audio.Music; 
import com.badlogic.gdx.graphics.GL30; 
import com.badlogic.gdx.graphics.OrthographicCamera; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.BitmapFont; 
import com.badlogic.gdx.graphics.g2d.Sprite; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; 
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter; 
import com.badlogic.gdx.math.Vector3; 

public class CatchEggs implements ApplicationListener { 
    public static final int SCREEN_HEIGHT = 800; 
    public static final int SCREEN_WIDTH = 480; 
    public static final float FRAME_SPEED = .1f; 
    public static int shortWastedCount,eggCatchedCount; 
    private int score; 
    private String yourScoreName; 
    BitmapFont yourBitmapFontName; 

    public static ArrayList<Texture> TextureArray = new ArrayList<Texture>(); 

    private OrthographicCamera camera; 
    private SpriteBatch batch; 
    private AnimatedSprite basket; 
    private EggLayManager EggLayManager; 
    private Music gameMusic; 
    private Hen hen1,hen2,hen3; 
    private CollisionManager collisionManager; 
    private boolean isGameOver = false; 
    @Override 
    public void create() {  

     //Texture.setEnforcePotImages(false); 
     camera = new OrthographicCamera(); 
     camera.setToOrtho(false, SCREEN_WIDTH, SCREEN_HEIGHT); 

     batch = new SpriteBatch(); 
     Sprite basketSprite = new Sprite(TextureManager.BASKET_TEXTURE); 
     basket = new AnimatedSprite(basketSprite,FRAME_SPEED,1,1); 
     basket.setPosition(SCREEN_WIDTH/2, 0); 

     EggLayManager = new EggLayManager(TextureManager.EGG_TEXTURE); 

     TextureArray.add(TextureManager.GREEN_EGG_TEXTURE); 
     TextureArray.add(TextureManager.PINK_EGG_TEXTURE); 
     TextureArray.add(TextureManager.GOLDEN_EGG_TEXTURE); 
     TextureArray.add(TextureManager.EGG_TEXTURE); 

     hen1 = new Hen(TextureManager.HEN_TEXTURE, EggLayManager,0,SCREEN_WIDTH/3); 
     hen2 = new Hen(TextureManager.HEN_TEXTURE, EggLayManager,SCREEN_WIDTH/3,2*SCREEN_WIDTH/3); 
     hen3 = new Hen(TextureManager.HEN_TEXTURE, EggLayManager,2*SCREEN_WIDTH/3,SCREEN_WIDTH); 

     collisionManager = new CollisionManager(basket,EggLayManager); 

     gameMusic = Gdx.audio.newMusic(Gdx.files.internal("data/game-music.mp3")); 
     gameMusic.setVolume(.25f); 
     gameMusic.setLooping(true); 
     gameMusic.play(); 

     score = 0; 
     yourScoreName = "score: 0"; 
     yourBitmapFontName = new BitmapFont(); 

     FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("data/font.ttf")); 
     FreeTypeFontParameter parameter = new FreeTypeFontParameter(); 
     parameter.size = 27; 
     yourBitmapFontName = generator.generateFont(parameter); // font size 12 pixels 
     generator.dispose(); 
    } 

    @Override 
    public void dispose() { 
     batch.dispose(); 
     gameMusic.dispose(); 
     yourBitmapFontName.dispose(); 
     gameMusic.dispose(); 
     yourBitmapFontName.dispose(); 
     TextureManager.BACKGROUND.dispose(); 
     TextureManager.BASKET_TEXTURE.dispose(); 
     TextureManager.GAME_OVER.dispose(); 
     TextureManager.EGG_TEXTURE.dispose(); 
     TextureManager.EGG_BREAK_TEXTURE.dispose(); 
     TextureManager.HEN_TEXTURE.dispose(); 
     TextureManager.SCORE_BOARD_BOUNDARY.dispose(); 
     TextureManager.GOLDEN_EGG_TEXTURE.dispose(); 
     TextureManager.PINK_EGG_TEXTURE.dispose(); 
     TextureManager.GREEN_EGG_TEXTURE.dispose(); 
     TextureArray.clear(); 
    } 

    @Override 
    public void render() {  
     Gdx.gl.glClearColor(1, 1, 1, 1); 
     Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT); 
     batch.setProjectionMatrix(camera.combined); 
     batch.begin(); 
     batch.draw(TextureManager.BACKGROUND,0,0,SCREEN_WIDTH,SCREEN_HEIGHT); 

     showImage(TextureManager.SCORE_BOARD_BOUNDARY,SCREEN_WIDTH/2 - TextureManager.SCORE_BOARD_BOUNDARY.getWidth()/2, 
                 SCREEN_HEIGHT - TextureManager.SCORE_BOARD_BOUNDARY.getHeight()); 

     hen1.draw(batch); 
     hen2.draw(batch); 
     hen3.draw(batch); 
     EggLayManager.draw(batch); 
     basket.draw(batch); 

     showScore(); 
     showGameOver(); 

     batch.end(); 
     handleInput(); 

     if(!isGameOver) { 
      basket.move(); 
      hen1.update(); 
      hen2.update(); 
      hen3.update(); 
      EggLayManager.update(); 
      collisionManager.handleCollisions(); 
     } 

     if(basket.isCaught()) { 
      isGameOver = false; 
     }else{ 
      isGameOver = true; 
     } 

    } 

    private void showGameOver() { 
     if(shortWastedCount>=21) isGameOver = true; 

     if(isGameOver){ 
      showImage(TextureManager.GAME_OVER,SCREEN_WIDTH/2 - TextureManager.GAME_OVER.getWidth()/2, SCREEN_HEIGHT/2 - TextureManager.GAME_OVER.getHeight()/2); 
     } 
    } 

    private void showImage(Texture t,int x, int y) { 
     batch.draw(t, x, y); 
    } 

    private void showScore() { 

     showImage(TextureManager.EGG_TEXTURE,  39 - TextureManager.EGG_TEXTURE.getWidth()/2  , 750); 
     showImage(TextureManager.EGG_BREAK_TEXTURE, 39 - TextureManager.EGG_BREAK_TEXTURE.getWidth()/2 , 700); 

     yourScoreName = " " + eggCatchedCount; 
     yourBitmapFontName.setColor(1.0f, 1.0f, 1.0f, 1.0f); 
     yourBitmapFontName.draw(batch, yourScoreName, 72, 777);   

     yourScoreName = " " + shortWastedCount; 
     yourBitmapFontName.setColor(1.0f, 1.0f, 1.0f, 1.0f); 
     yourBitmapFontName.draw(batch, yourScoreName, 72, 725); 
    } 

    private void handleInput() { 
     if(Gdx.input.isTouched()) { 
      if(isGameOver) { 
       basket.setCaught(true); 
       isGameOver = false; 
       shortWastedCount=eggCatchedCount=0; 
      } 

      Vector3 touchPosition = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0); 
      camera.unproject(touchPosition); 

      if(touchPosition.x > basket.getX()) { 
       basket.moveRight(touchPosition); 
      } 
      else{ 
       basket.moveLeft(touchPosition); 
      } 
     } 
    } 

    @Override 
    public void resize(int width, int height) { 
    } 

    @Override 
    public void pause() { 
    } 

    @Override 
    public void resume() { 
    } 
} 
+0

你是否正確地在'ApplicationAdapter'或'Game'的'dispose()'回調中處理了事情? – EpicPandaForce 2014-09-10 11:26:25

+0

是的,我編輯了這個問題並添加了參考代碼。 – iappmaker 2014-09-10 11:32:43

+0

如果使用'Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);'會發生什麼情況? – EpicPandaForce 2014-09-10 14:24:49

回答

0

嘗試設置你的TextureManager爲null,資產經理,如果你有一個在裏面。總而言之,清理掉所有的靜態變量。

很高興我幫你。 乾杯。

+1

由於代碼有許多子類,我無法發佈完整的代碼。但是我所做的是在所有的子類中創建一個dispose()方法,在這個方法中,我將類的所有紋理字段都設置爲null。這些dispose()在主類中調用。而已。現在它就像一個魅力! – iappmaker 2014-09-12 01:32:48