Java游戏鼠标移动

use*_*684 0 java mouse user-interface awt mouseevent

我正在制作一个游戏,我们必须使用'捕手'来捕捉从窗户顶部掉下来的球.捕获只能向左/向右移动.

示例:http://puu.sh/xeq8

如果我想制作它以便我可以用鼠标移动'捕手',我应该朝哪个方向看?

现在,我有一个使用键盘的捕手 - 我使用KeyListener但是我不确定鼠标.

理想情况下,当鼠标在JPanel中移动时,我希望捕手能够向左/向右移动吗?或者那种东西是理想的.

tib*_*ibo 5

使用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())
   } 
}); 
Run Code Online (Sandbox Code Playgroud)