Program is not detecting mouse clicks

163 Views Asked by At

I am coding my very first game, but am having trouble getting my menu screen to work. Below is the code for my menu. I have been trying to test the exit button, but the program won't exit no matter where I click. I've also tried adding System.out.println(e.getX()+", "+e.getY()); to my mousePressed() method with no luck; it won't print anything out. I'm completely lost and could use any help. Sorry if this is a stupid question, I'm completely brand new to this! Thanks!

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();
    }
1

There are 1 best solutions below

0
On

It looks like you're using a MouseListener, which is meant for AWT, in a libgdx application, which won't work in the setup you have. Use Gdx.input to poll for input (or an InputListener).

You can also look at this tutorial: http://www.gamefromscratch.com/post/2013/10/15/LibGDX-Tutorial-4-Handling-the-mouse-and-keyboard.aspx