什么内存泄漏/损坏会导致使用 libgdx 崩溃?

问题描述 投票:0回答:1

我正在编写一个选择您自己的冒险游戏,其中每个游戏状态都是不同的屏幕类别。当您之前单击屏幕上的相应按钮时,您会创建该类的一个实例(将显示该实例)(想想亨利火柴人游戏,但要简单得多)。我不知道还能怎么做;这是我的第一个真正的游戏。

这是我迄今为止所上的两门课:

public class MarketScreen implements Screen {

    final Heist game;

    private final Texture backgroundImage;
    private final Music backgroundMusic;

    private Stage stage;
    private Table table;
    private TextButton button1;
    private TextButton button2;
    private TextArea text;

    private OrthographicCamera camera;

    public MarketScreen(final Heist game) {
        this.game = game;

        //load images (64x64 pixels)
        backgroundImage = new Texture("background.jpeg");

        //load sound effects and music
        backgroundMusic = Gdx.audio.newMusic(Gdx.files.internal("ironLung.mp3"));
        backgroundMusic.setLooping(true);

        //create camera and spriteBatch
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 800, 480);

        //stage setup
        stage = new Stage(new ScreenViewport());
        Gdx.input.setInputProcessor(stage);

        //table setup
        table = new Table();
        table.setFillParent(true);
        stage.addActor(table);
        table.pad(50);
        table.padTop(100);

        //button and text box setup
        String dialogue = "You turn around from the market stall to the sound of your name at a shout. It's your long-time friend and " +
                " fellow bounty hunter, Wesdru. There's an urgency to her mannerisms that raises your heart rate immediately, " +
                "your hand flying to your sword pommel at your hip. 'It's Lorena. Dras has her.' Keegin Dras, this lawless town's " +
                "most feared crime boss. 'What,' you intone breathlessly. 'She doesn't stoop to meddle with cowboys!' Wesdru grabs " +
                "your shoulders roughly and makes brutal eye contact. 'I don't know why. But I do know we have to go NOW. You and I " +
                "both know how cruel Dras can be.'\n\n\n\nYou only have enough time to buy one thing from the market. What do you take?";


        text = new TextArea(dialogue, Heist.skin);
        table.add(text).grow().colspan(2);

        table.row();

        button1 = new TextButton("Modded Grenade", Heist.skin);
        table.add(button1).width(200).expand();

        button2 = new TextButton("Lockpick", Heist.skin);
        table.add(button2).width(100).expand();

        button1.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                game.hasGrenade = true;
                game.setScreen(new OutsideScreen(game));
                dispose();
            }
        });

        button2.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                game.hasLockpick = true;
                game.setScreen(new OutsideScreen(game));
                dispose();
            }
        });
    }

    @Override
    public void show() { //called when screen is shown.
        backgroundMusic.play();
    }

    @Override
    public void render(float delta) {
        ScreenUtils.clear(1, 0, 0, 1);

        game.batch.begin();
        game.batch.draw(backgroundImage, 0, 0);
        game.batch.end();
        
        stage.draw();
    }

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

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {
        Gdx.input.setInputProcessor(null);
    }

    @Override
    public void dispose() {
        game.batch.dispose();
        backgroundImage.dispose();
        backgroundMusic.dispose();
        stage.dispose();

    }
}

public class OutsideScreen implements Screen {

    final Heist game;

    private final Texture backgroundImage;
    private final Music backgroundMusic;

    private TextButton button1;
    private TextButton button2;
    private TextButton button3;
    private TextArea text;

    private Stage stage;
    private Table table;

    private OrthographicCamera camera;

    public OutsideScreen(final Heist game) {
        this.game = game;

        //load images (64x64 pixels)
        backgroundImage = new Texture("state2.png");

        //load sound effects and music
        backgroundMusic = Gdx.audio.newMusic(Gdx.files.internal("Doom City.mp3"));
        backgroundMusic.setLooping(true);

        //create camera and spriteBatch
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 800, 480);


        //stage setup
        stage = new Stage(new ScreenViewport());
        Gdx.input.setInputProcessor(stage);

        //table setup
        table = new Table();
        table.setFillParent(true);
        stage.addActor(table);
        table.pad(50);
        table.padTop(100);

        //button and text box setup

        String dialogue = "Hello";

        text = new TextArea(dialogue, Heist.skin);
        table.add(text).grow().colspan(3);

        table.row();

        button1 = new TextButton("Storm them", Heist.skin);
        table.add(button1).width(150).expand();

        button2 = new TextButton("Sweet talk them", Heist.skin);
        table.add(button2).width(150).expand();

        button3 = new TextButton("Climb the building", Heist.skin);
        table.add(button3).width(175).expand();

        //STORM THEM
        button1.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                game.hasGrenade = true;
//                game.setScreen(new NEXTSCREEN(game));
//                dispose();
            }
        });

        //SWEET TALK THEM
        button2.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                game.hasLockpick = true;
//                game.setScreen(new NEXTSCREEN(game));
//                dispose();
            }
        });

        //CLIMB THE BUILDING
        button3.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                game.hasLockpick = false;
                game.hasGrenade = false;
            }
        });
    }

    @Override
    public void show() {
        backgroundMusic.play();
    }

    @Override
    public void render(float delta) {
        ScreenUtils.clear(1, 0, 0, 1);

        game.batch.begin();
        game.batch.draw(backgroundImage, 0, 0);
        game.batch.end();

        stage.draw();
    }

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

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {
        Gdx.input.setInputProcessor(null);
    }

    @Override
    public void dispose() {
        backgroundImage.dispose();
        backgroundMusic.dispose();
        stage.dispose();
    }
}

每次我单击 MarketScreen 实例中的按钮时,下一个屏幕就会崩溃。有时它会持续大约 15 秒,然后在没有背景图像的情况下再运行几次(只是在渲染方法中显示默认的屏幕填充),然后崩溃,但它总是崩溃。错误消息始终以 “Java 运行时环境检测到致命错误: SIGSEGV (0xb) 位于 pc=0x000000010d7fa85c,pid=22079,tid=23555 " 我认为这描述了通常由内存泄漏引起的分段错误。

可能是什么问题?为什么它会以不同的速度崩溃?

我最初将 TextButton、Stage、Table 和其他对象作为游戏属性,并简单地更改了它们在每个屏幕中的文本和排列,但我认为 Stage 可能是针对特定类编写的,所以我重构为每个屏幕类创建自己的对象。现在游戏通常崩溃速度变慢,但仍然会崩溃。

intellij-idea libgdx game-development
1个回答
0
投票

我认为你创造了意大利面。

错误行:

game.setScreen(new OutsideScreen(game));

描述:每次单击第二个线程时都会创建新类。然后在第一个(渲染)线程中进行绘制。竞赛发生在 2 个线程之间,天知道接下来会发生什么。

修复: 在 create() 方法(init)中调用它

OutsideScreen os = new OutsideScreen(game);
NEXTSCREEN ns = new NEXTSCREEN(game);

//to swap

game.setScreen(ns);
game.setScreen(os);
© www.soinside.com 2019 - 2024. All rights reserved.