I'm making a game in which we have to use 'Catchers' to catch balls which fall from the top of the window. Catches can only move left/right.
Example: http://puu.sh/xeq8
Which direction should I look/head into if I want to make it so that I can move a 'catcher' with a mouse?
Right now, I have a catcher that uses the keyboard - I used KeyListener however I am uncertain for the mouse.
Ideally, I'd like the catcher to move left/right when the mouse moves in the JPanel? Or something of that sort would be ideal.
2 Answers 2
Use a MouseMotionListener :
myPanel.addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent me) {
//move the catcher
//use me.getX() to have the horizontal position of the mouse
//eg : catcher.setX(me.getX())
}
});
Comments
Use MouseMotionListener to be notified of the position of mouse, I think you also need to click to capture the falling balls for that use MouseListener or MouseAdapter.
When your clicks your MouseListener will be notified and when they move their mouse poistion your MouseMotionListener callback will be triggered.
Comments
Explore related questions
See similar questions with these tags.