程序未检测到鼠标点击

时间:2014-07-26 03:14:20

标签: java libgdx mouse mousepress

我正在编写我的第一个游戏,但我无法使用我的菜单屏幕。以下是我的菜单代码。我一直试图测试退出按钮,但无论我点击哪里,程序都不会退出。我还尝试将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();
    }

1 个答案:

答案 0 :(得分:2)

看起来你在libgdx应用程序中使用了一个用于AWT的MouseListener,它在你所拥有的设置中无法工作。使用Gdx.input轮询输入(或InputListener)。

您还可以查看本教程:http://www.gamefromscratch.com/post/2013/10/15/LibGDX-Tutorial-4-Handling-the-mouse-and-keyboard.aspx