java如何在鼠标单击的瞬间打开和关闭布尔值?
我正在做这个项目,我有一个布尔连接到鼠标右键,有点像。因此,当我按下RMB时,布尔值将切换为true,但不会变回false,除非我再次按下它。有没有办法在瞬间打开/关闭布尔值?就像它保持一个滴答声,然后关闭? (正如您可能理解的那样,我对Java编码非常熟悉)
vvv代码
if(Component.isMouseRightClicked) {
if(block[x][y].id == Tile.underDirt || block[x][y].id == Tile.underDirtRight || block[x][y].id == Tile.underDirtLeft || block[x][y].id == Tile.underDirtTop || block[x][y].id == Tile.underDirtRight || block[x][y].id == Tile.underDirtBottom || block[x][y].id == Tile.underDirtBR || block[x][y].id == Tile.underDirtTR || block[x][y].id == Tile.underDirtTL || block[x][y].id == Tile.underDirtBL) {
if(sid != Tile.air) {
block[x][y].id = sid;
}
}
if(block[x][y].id == Tile.dirt || block[x][y].id == Tile.sand || block[x][y].id == Tile.stone) {
if(sid == Tile.dirt) {
block[x][y].id = Tile.dirtWall;
}
if(sid == Tile.stone) {
block[x][y].id = Tile.stoneWall;
}
if(sid == Tile.sand) {
block[x][y].id = Tile.sandWall;
}
}
}
这就是布尔值为真时发生的情况
vvv(通过鼠标单击)打开的布尔值vvv
public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON3) {
Component.isMouseRightClicked = true;
}
}
下面是我如何(悲惨地)尝试立即关闭它
public void mouseReleased(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1) {
Component.isMouseLeft = false;
Level.blockBreakTimer = 0;
} else if(e.getButton() == MouseEvent.BUTTON3) {
Component.isMouseRight = false;
Component.isMouseRightClicked = false;
Level.buildTimer = 0;
}
}
(我也有其他东西的按钮,但我想你只需要看看isMouseRightClicked)
谢谢你的帮助,非常感谢
# 1 楼答案
啊,对不起。起初我误解了你的问题
生成嵌套的if-else语句,然后在末尾将MouseClicked设置为false
(我现在在手机上,所以我把它缩短了。如果你愿意,我可以在回家后重新键入,但你得多等30多分钟。)