java – 如何在其他所有内容之前将变量从True变为False?

我有一个我需要的项目

>创建贴纸.
>点击它后贴纸会发出声音.
>让下一次点击复制并放置贴纸. (必须能够按照我想要的次数放置,而不必返回并再次单击原始贴纸.)
>能够点击另一个贴纸并选择要放置的贴纸.

在我的代码中,我的所有贴纸变量都是假的.一旦达到合适的条件,1个贴纸变量将成立,我将能够贴上贴纸.但是当我点击第二个贴纸时,第一个贴纸仍然是真的,并在第二个贴纸上贴了一个贴纸.我不希望这样.当我在第二个贴纸上释放鼠标左键时,如何将贴纸变量更改为false.我的代码使它在之后将其更改为false.我以前想要.请帮忙.谢谢.

boolean hatSoundPlay = false;
boolean bluntSoundPlay = false;
boolean dealwithitSoundPlay = false;
boolean weedSoundPlay = false;
while(true) { 
    int clickX = EZInteraction.getXMouse(); 
    int clickY = EZInteraction.getYMouse();  

    if (EZInteraction.wasMouseLeftButtonReleased()){ 

        if (hatPicture.isPointInElement(clickX, clickY)){
            bluntSoundPlay = false;
            weedSoundPlay = false;
            dealwithitSoundPlay = false;
            if (!hatSoundPlay) {
                hatsound.play(); 
                hatSoundPlay = true; 
            }
        }
        else if (backgroundPicture.isPointInElement(clickX, clickY) && hatSoundPlay == true && EZInteraction.wasMouseLeftButtonReleased()){ 
            EZ.addImage("hat.png", clickX, clickY);
                        hatsound.play();        
        }

        if (bluntPicture.isPointInElement(clickX, clickY)){ 
            hatSoundPlay = false;
            weedSoundPlay = false;
            dealwithitSoundPlay = false;
            if (!bluntSoundPlay) {
                    bluntsound.play(); 
                    bluntSoundPlay = true;
            } 
        }
        else if (backgroundPicture.isPointInElement(clickX, clickY) && bluntSoundPlay == true && EZInteraction.wasMouseLeftButtonReleased()){
            EZ.addImage("blunt.png", clickX, clickY);
                bluntsound.play();
        }

        if (dealwithitPicture.isPointInElement(clickX,  clickY)){                       hatSoundPlay = false;
             bluntSoundPlay = false;
             weedSoundPlay = false;
             if (!dealwithitSoundPlay) {
                dealwithitsound.play(); 
                dealwithitSoundPlay = true;
              } 
        }
        else if (backgroundPicture.isPointInElement(clickX, clickY) && dealwithitSoundPlay == true && EZInteraction.wasMouseLeftButtonReleased()){
             EZ.addImage("dealwithit.png", clickX, clickY);
                dealwithitsound.play();             
        }

        if (weedPicture.isPointInElement(clickX,  clickY)){ 
            dealwithitSoundPlay = false;
            hatSoundPlay = false;
            bluntSoundPlay = false;
            if (!weedSoundPlay) {
                weedsound.play(); //then weedsound will play
                weedSoundPlay = true;
            } 
        }   
        else if (backgroundPicture.isPointInElement(clickX, clickY) && weedSoundPlay == true && EZInteraction.wasMouseLeftButtonReleased()){
            EZ.addImage("weed.png", clickX, clickY);
                weedsound.play();
        }
    }

    EZ.refreshScreen();

}

最佳答案 首先,您应该检查该点是否在一个贴纸的区域内.如果没有,请检查另一个贴纸,依此类推.只有在检查完所有贴纸并设置相应的布线后,才能处理贴纸.

而不是4个布尔值,最好有一个变量代表当前活动的贴纸.

在伪代码中,类似于

if (point in hat) {
    set hat
} else if (point in blunt) {
    set blunt
} else if ...
  ...
} else {
    place the active sticker
}

所以你的代码看起来像这样:

    if (hatPicture.isPointInElement(clickX, clickY)){
        bluntSoundPlay = false;
        weedSoundPlay = false;
        dealwithitSoundPlay = false;
        if (!hatSoundPlay) {
            hatsound.play(); 
            hatSoundPlay = true; 
        }
    } else if (bluntPicture.isPointInElement(clickX, clickY)){ 
        hatSoundPlay = false;
        weedSoundPlay = false;
        dealwithitSoundPlay = false;
        if (!bluntSoundPlay) {
            bluntsound.play(); 
            bluntSoundPlay = true;
        } 
    } else if (dealwithitPicture.isPointInElement(clickX,  clickY)){
         hatSoundPlay = false;
         bluntSoundPlay = false;
         weedSoundPlay = false;
         if (!dealwithitSoundPlay) {
             dealwithitsound.play(); 
             dealwithitSoundPlay = true;
         } 
    } else if (weedPicture.isPointInElement(clickX,  clickY)){ 
         dealwithitSoundPlay = false;
         hatSoundPlay = false;
         bluntSoundPlay = false;
         if (!weedSoundPlay) {
             weedsound.play(); //then weedsound will play
             weedSoundPlay = true;
         } 
    } else if (backgroundPicture.isPointInElement(clickX, clickY) { 
        if (hatSoundPlay){ 
            EZ.addImage("hat.png", clickX, clickY);
            hatsound.play();        
        } else if (bluntSoundPlay){
            EZ.addImage("blunt.png", clickX, clickY);
            bluntsound.play();
        } else if (dealwithitSoundPlay){
            EZ.addImage("dealwithit.png", clickX, clickY);
            dealwithitsound.play();
        } else if (weedSoundPlay){
            EZ.addImage("weed.png", clickX, clickY);
            weedsound.play();
        }
    } 
点赞