0

我正在编写我的第一个游戏,但无法让我的菜单屏幕正常工作。下面是我的菜单的代码。我一直在尝试测试退出按钮,但无论我点击哪里,程序都不会退出。我也尝试过添加System.out.println(e.getX()+", "+e.getY());我的mousePressed()方法,但没有运气;它不会打印任何东西。我完全迷路了,可以使用任何帮助。抱歉,如果这是一个愚蠢的问题,我对此完全陌生!谢谢!

public class MenuScreen implements Screen {
    MyGame game;
    OrthographicCamera camera;
    SpriteBatch batch;

    public MenuScreen(MyGame game) {
        this.game = game;

        camera = new OrthographicCamera();
        camera.setToOrtho(false, 1080, 1920);

        batch = new SpriteBatch();
    }

    public class MouseInput implements MouseListener {

        @Override
        public void mouseClicked(MouseEvent arg0) {}

        @Override
        public void mouseEntered(MouseEvent arg0) {}

        @Override
        public void mouseExited(MouseEvent arg0) {}

        @Override
        public void mousePressed(MouseEvent e) {

            int mx = e.getX();
            int my = e.getY();

            /**
                batch.draw(Assets.sprite_startbutton, 165, 955);
                batch.draw(Assets.sprite_tutorialbutton, 325, 790);
                batch.draw(Assets.sprite_exitbutton, 365, 700);
                batch.end();
             */
            //exit button
            if (mx >= 365 && mx <= 600) {
                if (my >= 700 && my <= 775) {
                    //Pressed exit button
                    System.exit(1);
                }
            }
        }

        @Override
        public void mouseReleased(MouseEvent arg0) {}
    }

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

        camera.update();

        batch.setProjectionMatrix(camera.combined);

        batch.begin();
            batch.draw(Assets.texture_background, 0, 0);
            batch.draw(Assets.sprite_cat, 500, 1350, 750, 263); 
            //(500, 1350, 750, 263)[Cat on shelf]
            //(95, 1600, 900, 316)[Cat starting position]
            batch.draw(Assets.sprite_title, 50, 1600);
            batch.draw(Assets.sprite_startbutton, 165, 955);
            batch.draw(Assets.sprite_tutorialbutton, 325, 790);
            batch.draw(Assets.sprite_exitbutton, 365, 700);
        batch.end();
    }
4

1 回答 1

2

看起来您在 libgdx 应用程序中使用了一个用于 AWT 的 MouseListener,它在您的设置中不起作用。使用 Gdx.input 轮询输入(或 InputListener)。

你也可以看看这个教程:http ://www.gamefromscratch.com/post/2013/10/15/LibGDX-Tutorial-4-Handling-the-mouse-and-keyboard.aspx

于 2014-07-26T03:25:07.647 回答