0

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();
    }
David Conrad
  • 15,432
  • 2
  • 42
  • 54
  • Did you add the MouseListener to your component? – camickr Jul 26 '14 at 03:16
  • It looks like he's using libgdx (AWT doesn't have a Screen interface) – Selali Adobor Jul 26 '14 at 03:22
  • I'm sorry, I'm kind of new. What do you mean by that? I do have import java.awt.event.MouseListener; if that's what you mean. – user3785016 Jul 26 '14 at 03:23
  • If you take a look at my question's link it's explained in detail, but the simple version is you're currently mixing two different libraries. AWT is the library that has the MouseListener, it's for GUI applications with stuff like JFrame, JPanel, etc, it's not the best for making games by itself. LibGDX is a separate library focused on making games that has it's own way of dealing with the mouse. Right now you're using AWT's MouseListener, which is meant for use with other AWT components, with LibGDX's Screen class, which is not one of those components. You can follow that tutorial from page 1 – Selali Adobor Jul 26 '14 at 04:27
  • and it'll explain what you need to do to get started with LibGDX – Selali Adobor Jul 26 '14 at 04:27

1 Answers1

2

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

Selali Adobor
  • 2,060
  • 18
  • 30