重新打开LibGdx Android APP无法正确绘制屏幕 [英] Reopening the LibGdx Android APP not drawing the screens properly

查看:75
本文介绍了重新打开LibGdx Android APP无法正确绘制屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用LibGdx框架创建了一个android应用.每当我将其刷新到我的android设备时,它都能完美运行.但是,当我关闭该应用程序并重新打开它时,该应用程序显示黑色和白色矩形框,而不是图像.您能帮我解决这个问题吗?

I have created an android App using the LibGdx framework. Whenever I flash it to my android device it works perfectly. But When I close the app and open it freshly, the App shows the black and white rectangle boxes instead of the image. Could you please assist me in resolving this issue ?

代码

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() {
    }
}

推荐答案

请尝试将TextureManager设置为null,如果资产管理器设置为null,请尝试将其设置为null.总而言之,清除所有静态变量.

Try setting your TextureManager to null and assets manager if you have one in it. All in all clear up all static variables in dispose.

很高兴我帮助了您. 干杯.

Glad that I helped you. Cheers.

这篇关于重新打开LibGdx Android APP无法正确绘制屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆