代码之家  ›  专栏  ›  技术社区  ›  iappmaker

重新打开LibGdx Android应用程序,无法正确绘制屏幕

  •  2
  • iappmaker  · 技术社区  · 10 年前

    我已经使用LibGdx框架创建了一个android应用程序。每当我把它闪到我的android设备上时,它都能完美工作。但当我关闭应用程序并重新打开时,应用程序显示的是黑白矩形框,而不是图像。你能帮我解决这个问题吗? enter image description here

    密码

    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() {
        }
    }
    
    1 回复  |  直到 10 年前
        1
  •  0
  •   Veljko    10 年前

    尝试将TextureManager设置为null,如果有资产管理器,则将其设置为assets manager。总而言之,将清除dispose中的所有静态变量。

    很高兴我帮助了你。 干杯