我在这个项目上工作,我有一个布局连接到鼠标右键,有点像。因此,当我按下RMB时,布尔值会切换为true,但除非我再次按下它,否则不会改回false。有没有办法在瞬间打开/关闭布尔值?就像它保持1蜱,然后关闭? (我对Java编码很新鲜,你可能会理解)
vvv Code 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打开布尔值(通过mouseClicked)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)
感谢您的帮助,非常感谢。
答案 0 :(得分:0)
创建嵌套if else语句,然后在最后将MouseClicked设置为false。
if(mouseRclick) {
if(block[x][y].isUnder()) {
if(sid != Tile.air) {
block[x][y].id = sid;
}
// added "else"
} else if(block[x][y].isGround()) {
block[x][y].id = toWall(sid);
}
// Makes sure that it only runs once.
mouseRclick = false;
}
(我现在在移动设备上,所以我缩短了它。如果你愿意的话,我可以在我回家时重新输入它,但你不得不再等30多分钟。)