我正在编写一个选择你自己的冒险游戏,其中每个游戏状态是一个不同的屏幕类。当你点击屏幕上的相应按钮时,你会创建一个类的示例(将显示)(想想亨利火柴人游戏,但要简单得多)。我不知道如何做到这一点;这是我的第一个真实的游戏。
以下是我目前为止的两个课程:
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();
}
}
个字符
每次我点击MarketScreen示例中的一个按钮,下一个屏幕就会崩溃。有时会持续大约15秒,然后在没有背景图像的情况下再运行几秒钟(只是在render方法中显示默认的屏幕填充)然后崩溃,但它总是崩溃。错误消息总是以
A fatal error has been detected by the Java Runtime Environment:
SIGSEGV (0xb) at pc=0x000000010d7fa85c, pid=22079, tid=23555
型
我相信它描述了一个分段错误,通常是由内存泄漏引起的。
可能是什么问题?为什么它会以不同的速度坠毁?
我最初将TextButton、Stage、Table和其他对象作为游戏属性,并简单地更改了它们在每个屏幕中的文本和排列,但我认为Stage可能是编写为特定于类的,所以我重构了每个屏幕类以创建自己的对象。游戏现在通常崩溃得更慢,但它仍然崩溃。
编辑:由于我希望屏幕能够根据跟踪游戏进度的布尔值进行调整,所以我没有提前创建所有屏幕。相反,我最终将创建按钮和创建侦听器的代码放在show()中,并从hide()调用dispose()(因为hide是更改屏幕时自动调用的唯一方法),这似乎有效!
1条答案
按热度按时间pbpqsu0x1#
我认为你创造了意大利面。
错误行:
字符串
**说明:**你创建新的类,每次你点击第二线程。然后在第一(渲染)线程你画。赛车之间发生2线程和上帝知道接下来会发生什么。
**修复:**在create()方法(init)中调用this
型
单击按钮时删除dispose()。
要dispose在class >>public class MyGdxGame中调用它,需要在dispose()中扩展ApplicationAdapter<<。