简体   繁体   English

拖动后的Java鼠标

[英]Java Mouse After a Drag

I'm making a small game project in Java. 我正在用Java开发一个小型游戏项目。 In it you have a character that shoots in the direction of the mouse when "pressed" or "dragged" (you know, in Java's terms). 在其中,您有一个角色,当“按下”或“拖曳”时,会朝着鼠标的方向射击(用Java的术语来说)。 The only problem is that if you stop dragging but you still hold the left mouse button down you stop shooting. 唯一的问题是,如果您停止拖动但仍然按住鼠标左键,则将停止拍摄。

Is there a way to detect if the mouse button is down after a drag? 有没有一种方法可以检测拖动后鼠标按钮是否按下?

NOTE: the mouse is not sensed as "pressed" after the drag. 注意:拖动后,鼠标不会被感知为“按下”。

You will get the information when a mouse button is pressed and when it is released again. 按下鼠标按钮并再次释放它时,您将获得该信息。 If you want to know the state in between, you need to use a boolean to store that information. 如果您想知道两者之间的状态,则需要使用布尔值来存储该信息。

Example: 例:

final boolean[] buttonStates = new boolean[3];

public void mousePressed(MouseEvent e) {
  buttonStates[e.getButton()] = true;
}

public void mouseReleased(MouseEvent e) {
  buttonStates[e.getButton()] = false;
}

You would do the same for keyboard input by the way. 顺便说一句,您将对键盘输入执行相同的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM