Meeesh
Meeesh

Reputation: 1042

In Java, my MouseMotionListener and MouseListener is detecting clicks, but not movement

so I am creating a java game and and here is my mouse's clicks are being detected but the mouseMoved is not being run at all. There is a print statement in the method which never gets run. I am really stuck because since the clicks are being registered across the window, there shouldn't be anything wrong with the mouse motion.

Is the mouse dependent on anything else because I really have no idea what is causing this.

Thanks.

public class Mouse implements MouseListener, MouseMotionListener{

    private static int mouseX = -1;
    private static int mouseY = -1;
    private static int mouseB = -1;

    public static int getX(){
        return mouseX;
    }

    public static int getY(){
        return mouseY;
    }

    public static int getB(){
        return mouseB;
    }



    public void mouseMoved(MouseEvent e) {
        System.out.println("Mouse Moved");
        mouseX = e.getX();
        mouseY = e.getY();
    }

    public void mouseEntered(MouseEvent e) {}

    public void mouseExited(MouseEvent e) {}

    public void mousePressed(MouseEvent e) {
        mouseB = e.getButton();
        System.out.println(e.getX() + " " + e.getY());
    }

    public void mouseReleased(MouseEvent e) {
        mouseB = -1;
    }

    public void mouseDragged(MouseEvent e) {}
    public void mouseClicked(MouseEvent e) {

    }
}

Here is my game constructor which initiates everything.

public Game(){
        //cCursor();

        screen = new Screen(WIDTH, HEIGHT);
        mouse = new Mouse();
        keys = new Keyboard();
        //level = new RandomLevel(64, 64);
        level = Level.spawn;
        TileCoord pSpawn = new TileCoord(20,66);
        player = new Player(pSpawn.x(), pSpawn.y(), keys);
        player.init(level);

        addKeyListener(keys);
        addMouseListener(mouse);
    }

Upvotes: 0

Views: 75

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

MouseMoitionListener is a different listener to MouseListener and needs to be registered separately...

Start by adding a call to addMouseMotionListener

addMouseListener(mouse);
addMouseMotionListener(mouse);

Have a look at How to Write a Mouse Listener for more details

Upvotes: 3

Related Questions