-1
我正在研究這個項目,並且我有一個連接到鼠標右鍵的布爾類型。所以當我按下人民幣時,布爾切換爲真,但不會變回爲假,除非我再次按下它。有沒有辦法在瞬間打開/關閉布爾值?就像它保持1刻度,然後關閉? (我很新鮮的Java代碼,正如你可能知道)如何在點擊鼠標的瞬間打開和關閉布爾值?
VVV代碼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;
}
}
}
也就是說當布爾值爲true
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)
感謝您的幫助,真的很感激。
使用擺動計時器 – MadProgrammer
可能的錯誤可能是使用鼠標滾動的功能,因爲當您按下並釋放鼠標點擊時會調用它。你應該嘗試mousepressed而不是鼠標點擊 –