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
16.5k3 gold badges49 silver badges60 bronze badges
asked Jul 26, 2014 at 3:14
5
  • Did you add the MouseListener to your component? Commented Jul 26, 2014 at 3:16
  • It looks like he's using libgdx (AWT doesn't have a Screen interface) Commented Jul 26, 2014 at 3: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. Commented Jul 26, 2014 at 3: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 Commented Jul 26, 2014 at 4:27
  • and it'll explain what you need to do to get started with LibGDX Commented Jul 26, 2014 at 4:27

1 Answer 1

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

answered Jul 26, 2014 at 3:25
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.