在按下后退按钮后打开应用程序时,Libgdx纹理很奇怪?

时间:2015-09-10 21:51:47

标签: android libgdx scene2d

所以这就是我的困境,我真的很接近在游戏商店发布我的游戏。

在设备上测试我的应用时,我意识到点击主页按钮后,再次打开应用。一切都处理得很好。

然而,当按下后退按钮并再次打开应用程序时,我游戏中的纹理加载很奇怪,就像真的很奇怪。

Heres是应该看起来像什么的屏幕截图以及它不应该是什么......

这是显示应该是什么样子的链接 http://imgur.com/20QS2bx,bGyhbDJ#0

这是按下后退按钮后加载的那个http://imgur.com/20QS2bx,bGyhbDJ#1

经过一段时间的测试后,我认为问题在于,当单击后退按钮时,它会触发一个“退出”类型的功能,该功能应该退出应用程序。但是,由于某种原因,应用程序仍然存在于手机的背景中,导致在单击后退按钮后加载奇怪的纹理。如果我是正确的,我需要做什么才能处理这种行为。

public class MainMenuScreen implements Screen{
    private static final int FLOOR_Y_OFFSET = -150;
    private static final int FOREGROUND_Y_OFFSET = -150;

    private FishyDash game;
    private OrthographicCamera cam;

    private Texture background;
    private Texture foreground;
    private Vector2 foregroundPos1, foregroundPos2;
    private Texture floor;
    private Vector2 floorPos1, floorPos2;
    private Fish fishy;

    //TABLE STUFF
    private Table table;
    private Table buttonTable;
    private Stage stage;

    private Image game_title;
    private Skin skin;
    private TextureAtlas buttonAtlas;
    private TextButton buttonPlay, buttonRate;
    private Viewport gamePort;

    public BitmapFont font;

    //Load all the sounds needed
    public Sound jumpSound;
    public Sound buttonClicked;
    public Music backgroundMusic;

    GameState gameState;

    //On Pause stuff
    public static Vector2 fish_pause_pos;

    public MainMenuScreen(FishyDash game) {
        this.game = game;
        gameState = GameState.READY;

        System.out.println("first");
    }


    @Override
    public void show() {
        System.out.println("second");
        //Initialize all the sounds
        jumpSound = Assets.manager.get(Assets.jumpSound, Sound.class);
        buttonClicked = Assets.manager.get(Assets.buttonClicked, Sound.class);
        backgroundMusic = Assets.manager.get(Assets.backgroundMusic, Music.class);
        backgroundMusic.setLooping(true);
        backgroundMusic.setVolume(.5f);
        backgroundMusic.play();

        //Initialize the textures needed in the MainMenuScreen class
        background = Assets.manager.get(Assets.background, Texture.class);
        floor = Assets.manager.get(Assets.floor, Texture.class);
        foreground = Assets.manager.get(Assets.foreground, Texture.class);

        fishy = new Fish();
        fishy.setPos(80, foreground.getHeight() + FOREGROUND_Y_OFFSET - 150);
        fishy.setMOVEMENT_X(200);
        fishy.setGRAVITY(-40);

        cam = new OrthographicCamera();
        cam.setToOrtho(false, background.getWidth(), background.getHeight());

        foregroundPos1 = new Vector2(cam.position.x - (cam.viewportWidth / 2), 0);
        foregroundPos2 = new Vector2(cam.position.x - (cam.viewportWidth / 2) + foreground.getWidth(), 0);

        floorPos1 = new Vector2(cam.position.x - (cam.viewportWidth / 2), 0);
        floorPos2 = new Vector2(cam.position.x - (cam.viewportWidth / 2) + floor.getWidth(), 0);

        createTable();

//        Gdx.input.setCatchBackKey(true);
//        game.getAdsController().showBannerAd();
    }

    public void createTable() {
        gamePort = new StretchViewport(FishyDash.V_WIDTH, FishyDash.V_HEIGHT, cam);
        stage = new Stage(gamePort, game.batch);
        stage.getViewport().setCamera(cam);

        table = new Table();
        table.setFillParent(true);
        table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        buttonTable = new Table();
        buttonTable.setFillParent(true);
        buttonTable.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        game_title = new Image(new Texture("game_title.png"));
        font = Assets.manager.get(Assets.font, BitmapFont.class);
        buttonAtlas = Assets.manager.get(Assets.buttonAtlas, TextureAtlas.class);
        skin = new Skin(buttonAtlas);

        //Creating the TextStyle for the playButton
        TextButton.TextButtonStyle playButtonStyle = new TextButton.TextButtonStyle();
        playButtonStyle.up = skin.getDrawable("button_up");
        playButtonStyle.down = skin.getDrawable("button_down");
        playButtonStyle.pressedOffsetX = 1;
        playButtonStyle.pressedOffsetY = -1;
        playButtonStyle.font = font;

        //Creating the TextStyle for the rateButton
        TextButton.TextButtonStyle rateButtonStyle = new TextButton.TextButtonStyle();
        rateButtonStyle.up = skin.getDrawable("button_up");
        rateButtonStyle.down = skin.getDrawable("button_down");
        rateButtonStyle.pressedOffsetX = 1;
        rateButtonStyle.pressedOffsetY = -1;
        rateButtonStyle.font = font;

        //Setting the style and text for the playButton
        buttonPlay = new TextButton("PlAY", playButtonStyle);
        buttonPlay.pad(10);

        //Setting the style and text for the rateButton
        buttonRate = new TextButton("RATE", rateButtonStyle);
        buttonRate.pad(10);

        table.top();
        table.add(game_title).padTop(175).expandX();

        buttonTable.bottom().padBottom(200);
        buttonTable.add(buttonPlay).expandX().padLeft(30);
        buttonTable.add(buttonRate).expandX().padRight(30);

//        table.debug();
        stage.addActor(table);
        stage.addActor(buttonTable);

        buttonPlay.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                buttonClicked.play();
                dispose();
                game.setScreen(new GameScreen(game));
            }
        });

        buttonRate.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                buttonClicked.play();
                System.out.println("Nothing happens Yet");
            }
        });
    }

    public void updateWorld(float delta) {

        switch (gameState) {
            case READY:

                fishy.update(delta);

                if (fishy.getPosition().y < foreground.getHeight() + FOREGROUND_Y_OFFSET - 35) {
                    fishy.setMOVEMENT_X(100);
                }

                if (fishy.getPosition().y < floor.getHeight() + FLOOR_Y_OFFSET + 150) {
                    fishy.setMOVEMENT_X(100);
                    fishy.normalJump();
                    jumpSound.stop();
                }

                updateForeground();
                updateFloor();

                cam.position.x = fishy.getPosition().x + (cam.viewportWidth / 2) - 80;
                cam.update();

                table.setPosition(cam.position.x - cam.viewportWidth / 2, 0);
                buttonTable.setPosition(cam.position.x - cam.viewportWidth / 2, 0);

                Gdx.input.setInputProcessor(stage);
                break;

            case PAUSED:
                gameState = GameState.READY;
                break;
        }
    }

    public void drawWorld() {
        game.batch.setProjectionMatrix(cam.combined);
        switch (gameState) {

            case READY:
                game.batch.begin();

                game.batch.draw(background, cam.position.x - cam.viewportWidth / 2, 0);

                game.batch.draw(foreground, foregroundPos1.x, foregroundPos1.y + FOREGROUND_Y_OFFSET);
                game.batch.draw(foreground, foregroundPos2.x, foregroundPos2.y + FOREGROUND_Y_OFFSET);

                game.batch.draw(floor, floorPos1.x, floorPos1.y + FLOOR_Y_OFFSET);
                game.batch.draw(floor, floorPos2.x, floorPos2.y + FLOOR_Y_OFFSET);

                game.batch.draw(fishy.getAnimation().getKeyFrame(fishy.timePassed, true),
                        fishy.getPosition().x, fishy.getPosition().y);

                game.batch.end();

                stage.act();
                stage.draw();
                break;

            case PAUSED:
                break;
        }
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        updateWorld(delta);
        drawWorld();
    }

    @Override
    public void dispose() {
        System.out.println("Main Menu Dispose Called");

        fishy.dispose();
        stage.dispose();
    }

    public void updateForeground() {
        if(cam.position.x - (cam.viewportWidth / 2) > foregroundPos1.x + foreground.getWidth())
            foregroundPos1.add(foreground.getWidth() * 2, 0);
        if(cam.position.x - (cam.viewportWidth / 2) > foregroundPos2.x + foreground.getWidth())
            foregroundPos2.add(foreground.getWidth() * 2, 0);
    }

    public void updateFloor() {
        if(cam.position.x - (cam.viewportWidth / 2) > floorPos1.x + floor.getWidth())
            floorPos1.add(floor.getWidth() * 2, 0);
        if(cam.position.x - (cam.viewportWidth / 2) > floorPos2.x + floor.getWidth())
            floorPos2.add(floor.getWidth() * 2, 0);
    }

    @Override
    public void resize(int width, int height) {
        stage.getViewport().update(width, height, true);
    }

    @Override
    public void pause() {
        System.out.println("pause");
        gameState = GameState.PAUSED;
    }

    @Override
    public void resume() {
        System.out.println("resume");
    }

    @Override
    public void hide() {
        System.out.println("hide");
    }

    public enum GameState {
        READY, PAUSED;
    }
}

修改

据我所知,将资产管理器更改为下面的内容应该可以正常工作,但是当我这样做时,我不能再使用管理器了,它会以红色突出显示并给出错误非静态字段管理器不能从非静态内容引用?

AssetManager manager = new AssetManager();

    public static final String font = "fonts/mario.fnt";

    public static final String jumpSound = "sounds/jumpSound.wav";
    public static final String buttonClicked = "sounds/buttonClicked.wav";
    public static final String passSound = "sounds/passSound.wav";
    public static final String gameOver = "sounds/gameOver.mp3";
    public static final String backgroundMusic = "sounds/backgroundMusic.wav";

    public static final String background = "background.png";
    public static final String floor = "floor.png";
    public static final String foreground = "foreground.png";

    public static final String fishAtlas = "sprite_animations/fishy_sprite_ani.atlas";
    public static final String buttonAtlas = "ui/buttons.atlas";


    public Assets() {
    }

    public static void load() {
        manager.load(font, BitmapFont.class);

        manager.load(jumpSound, Sound.class);
        manager.load(buttonClicked, Sound.class);
        manager.load(passSound, Music.class);
        manager.load(gameOver, Music.class);
        manager.load(backgroundMusic, Music.class);

        manager.load(background, Texture.class);
        manager.load(floor, Texture.class);
        manager.load(foreground, Texture.class);

        manager.load(fishAtlas, TextureAtlas.class);
        manager.load(buttonAtlas, TextureAtlas.class);

    }

1 个答案:

答案 0 :(得分:0)

即使您关闭应用,背景中存在的对象的静态实例也存在问题。

当应用程序打开资产管理器的所有实例时,请确保您正在处置,归零和创建新文件。

使用

AssetManager manager;

...

manager.clear();

如果你使用了一些处理皮肤,并且如果你使用一些作为单例,那么将 null 设置为处理程序