Java游戏鼠标移动

时间:2012-05-28 05:33:06

标签: java user-interface mouse awt mouseevent

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

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

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

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

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

2 个答案:

答案 0 :(得分: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())
   } 
}); 

答案 1 :(得分:3)

使用MouseMotionListener通知鼠标的位置,我认为您还需要点击以捕获该使用的落球[{1}}或MouseListener

当您点击时,系统会通知您MouseAdapter,当他们移动鼠标时,系统会触发MouseListener回调。

  1. How to work with MouseListener
  2. How to work with MouseMotionListener
相关问题