How to make a variable go from True to False before everything else?

I have a project where I need

  • Create a sticker.
  • Clicking on it, stick the sound.
  • Repeat the next click and place the sticker. (You need to be able to post as many times as I want without returning and clicking on the original sticker again.)
  • Be able to click on another sticker and choose it for placement.

In my code, I have all the variable stickers starting as false. After the correct conditions are met, 1 sticker variable will be displayed, and I can place the sticker. But when I click on the second sticker, the first sticker is still correct and puts the sticker on the second sticker. I do not want it. How to change the sticker variable to false when I release the left mouse button on the second sticker. My code changes it to false after. I want earlier. Please help. Thanks.

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
source share
1 answer

First you need to check if the dot is in the area of ​​one sticker. If not, check another sticker and so on. Only after checking all the stickers and installing the appropriate Boolean you should deal with the placement of the stickers.

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();
        }
    } 
+2

Source: https://habr.com/ru/post/1607213/


All Articles