java - 更换屏幕时 Libgdx 卡住

标签 java libgdx screens

我找不到解决方案,我现在正在寻找几个小时.. 当我更换屏幕时,一切都会卡住。我不知道出了什么问题。一切都应该好起来。是因为内存丢失还是循环在某个地方停止了?

我的代码:

public class Main extends Game {

    MainMenuOne mainmenu;
    public GameScreen gamescreen;
    private static boolean change;
    @Override
    public void create() {
        System.out.println("ZBGame Created!");
        AssetLoader.load();
        setScreen(new MainMenuOne(null));

    }


    @Override
    public void dispose() {

        super.dispose();
        //AssetLoader.dispose();
    }

}







public class MainMenuOne implements ApplicationListener, Screen {
    private GameWorld world;
    private MenuRenderer renderer;
    private float runTime;
    private Player player;
    private MoveButton movebutton;
    private bUp bup;

    private static int buttonsize;

    public static Stage stage; //** stage holds the Button **//
    static BitmapFont font; //** same as that used in Tut 7 **//
    public static int seed;
    private TextureAtlas buttonsAtlas; //** image of buttons **//
    private Skin buttonSkin; //** images are used as skins of the button **//
    private TextButton button; //** the button - the only actor in program **//


    private OrthographicCamera cam;

    // This is the constructor, not the class declaration
    public MainMenuOne(final Main main) {

        final Random r = new Random();
        buttonsAtlas = new TextureAtlas("ui/buttons.pack"); //** button atlas image **// 
        buttonSkin = new Skin();
        buttonSkin.addRegions(buttonsAtlas); //** skins for on and off **//
        font = new BitmapFont(Gdx.files.internal("ui/default.fnt"),false); //** font **//

        stage = new Stage();        //** window is stage **//
        stage.clear();
        Gdx.input.setInputProcessor(stage); //** stage is responsive **//

        TextButtonStyle style = new TextButtonStyle(); //** Button properties **//
        style.up = buttonSkin.getDrawable("button.up.9.png");
        style.down = buttonSkin.getDrawable("button.down.9.png");
        style.font = font;


        buttonsize = Gdx.graphics.getWidth()/6;
        float screenWidth = Gdx.graphics.getWidth();
        float screenHeight = Gdx.graphics.getHeight();
        float gameWidth = Gdx.graphics.getWidth()/2;
        float gameHeight = screenHeight / (screenWidth / 136);


        world = new GameWorld();
        renderer = new MenuRenderer(world,(int) gameHeight);
        player = new Player(world, (int) gameHeight);

        bup = new bUp (world, (int) gameHeight);

        Table table=new Table();
        table.setSize(gameWidth,gameHeight);




        movebutton = new MoveButton(world, (int) gameHeight);
        Gdx.input.setInputProcessor(new InputHandler(world));
        Gdx.input.setInputProcessor(stage);



        button = new TextButton( "S", style); //** Button text and style **//
        button.setPosition(screenWidth-buttonsize*2, 0); //** Button location **//
        button.setHeight(buttonsize); //** Button Height **//
        button.setWidth(buttonsize); //** Button Width **//





        button.addListener(new InputListener() {
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
                main.setScreen(new GameScreen(null));
                dispose();  

                   return true;
            }

            public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
                bUp.noJudam();
            }
        });




        stage.addActor(button);

    }

    @Override
    public void render(float delta) {

        //System.out.println(Gdx.graphics.getWidth()/2);
        stage.act();
        player.update(delta);
        movebutton.update(delta);
        runTime += delta;
        bup.update(delta);
        world.update(delta);
        renderer.render(runTime);
    }

    @Override
    public void resize(int width, int height) {
        System.out.println("GameScreen - resizing");
    }

    @Override
    public void show() {
        System.out.println("GameScreen - show called");

    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {
        System.out.println("GameScreen - pause called");
    }

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

    @Override
    public void dispose() {
        //this.dispose();
        buttonSkin.dispose();
        buttonsAtlas.dispose();
        font.dispose();
        stage.dispose();

    }

    @Override
    public void create() {
        // TODO Auto-generated method stub

    }

    @Override
    public void render() {
        // TODO Auto-generated method stub

    }


}

最佳答案

您在此处传递 null 作为参数:

setScreen(new MainMenuOne(null));

这是一个坏主意,因为构造函数需要一个 Game 对象作为参数。试试这个:

setScreen(new MainMenuOne(this));
<小时/>

顺便说一句,这个屏幕也是如此:

main.setScreen(new GameScreen(null));

您应该再次传递对“主”游戏的引用:

main.setScreen(new GameScreen(main));

关于java - 更换屏幕时 Libgdx 卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26537955/

相关文章:

android - 引用另一个类时出现 Libgdx Screen NullPointerException

java - 在java字符串中添加引号

android - SpriteBatch.setBlendFunction() 对 Android 没有影响

java - libGDX,检测矩形之间的侧面接触(侧面碰撞)

java - 球员移动(跳跃问题)

Java 游戏状态管理

java - 尝试为 Spring、Spring WebFlow、Hibernate 等设置我的第一个 Maven POM,但出现错误

Java调整数组中的平均值

java - JSP 中的 ReCaptcha