简体   繁体   中英

please tell me if there is a command for if () then

I am doing a project where I need to click on a picture, have a sound played, then click anywhere on the screen and have the sticker replicated and placed there. The sticker will keep being placed every time I release left click. Click on a new sticker and the process begins again.

I got to the sound, but I do not know how to do a then command. For example, if (released on sticker) sound.play(); then the next click will place sticker.

How do I write "then the next click will place sticker"?

Here is the code I have so far. I know the second add image won't work but I don't know what to do, so it is there for experiments.

import java.awt.Color;

public class pls {

public static void main(String[] args) {

        EZ.initialize(1644,1022); //pixel size of run screen

        EZImage picPicture = EZ.addImage("background.png", EZ.getWindowWidth() / 2, EZ.getWindowHeight() / 2); //set position of background

        EZImage rectanglePicture = EZ.addImage("rectangle.png", EZ.getWindowWidth() / 2, EZ.getWindowHeight () / 7); //set position of palette

        EZImage hatPicture = EZ.addImage("hat.png",1* EZ.getWindowWidth() / 10, EZ.getWindowHeight () / 6); //set position of stickers

        EZImage bluntPicture = EZ.addImage("blunt.png",1* EZ.getWindowWidth() / 3, EZ.getWindowHeight () / 6); //set position of stickers

        EZImage dealwithitPicture = EZ.addImage("dealwithit.png",3* EZ.getWindowWidth() / 5, EZ.getWindowHeight () / 6); //set position of stickers

        EZImage weedPicture = EZ.addImage("weed.png",10* EZ.getWindowWidth() / 11, EZ.getWindowHeight () / 6); //set position of stickers

        EZSound hatsound = EZ.addSound("airhorn.wav");
        EZSound bluntsound = EZ.addSound("yungdog.wav");
        EZSound dealwithitsound = EZ.addSound("sandstorm.wav");
        EZSound weedsound = EZ.addSound("weed.wav");

        while(true) { //while loop is always true
            int clickX = EZInteraction.getXMouse(); // declare an integer variable called clickX and the X mouse coordinate integer is assigned to it
            int clickY = EZInteraction.getYMouse(); // declare an integer variable called clickY and assign to it the Y mouse coordinate integer

            if (EZInteraction.wasMouseLeftButtonReleased()){  //if the left mouse button is released then
                if (hatPicture.isPointInElement(clickX, clickY)) //if the left mouse button is release on this picture
                    hatsound.play(); //then hatsound will play

                if (bluntPicture.isPointInElement(clickX, clickY)) //if the left mouse button is released on this picture
                    bluntsound.play(); //then bluntsound will play

                if (dealwithitPicture.isPointInElement(clickX,  clickY)) //if the left mouse button is released on this picture
                    dealwithitsound.play(); //then dealwithitsound will pay

                if (weedPicture.isPointInElement(clickX,  clickY)) //if the left mouse button is released on this picture
                    weedsound.play(); //then weedsound will play
        }

        while(true) {               
            if (EZInteraction.wasMouseLeftButtonReleased()){
                if (hatPicture.isPointInElement(clickX, clickY))
                    EZ.addImage("hat.png", clickX, clickY);

                if (bluntPicture.isPointInElement(clickX, clickY))
                    EZ.addImage("blunt.png", clickX, clickY);

                if (dealwithitPicture.isPointInElement(clickX, clickY))
                    EZ.addImage("dealwithit.png", clickX, clickY);

                if (weedPicture.isPointInElement(clickX, clickY))
                    EZ.addImage("weed.png", clickX, clickY);
            }
            EZ.refreshScreen();
        }
      }
    }
}

First, if you want to have any real clarity while you're obviously not far into programming, refrain from using shorthand if statements. Use brackets, lots of them. Use whitespace, lots of it. There is no reason to optimize through removing brackets and whitespaces and you're not paying for the extra lines like you would on physical paper.

I'm not going to point out all the obvious problem with your code styling because at that point I may as well refer you to the amazing Head First Java book, but you get the idea.

Second, I'd very much suggest storing the currently selected image (or, since Java is pass by reference, a reference to that image) and then any time a user clicks, that click is not on an image and an image is selected, place a copy of the image on screen.

Example;

EZ.initialize(1644, 1022); //pixel size of run screen
EZImage selectedImage;

EZImage rectanglePicture = EZ.addImage("rectangle.png", EZ.getWindowWidth() / 2, EZ.getWindowHeight() / 7);
EZImage hatPicture = EZ.addImage("hat.png", 1 * EZ.getWindowWidth() / 10, EZ.getWindowHeight() / 6);

EZSound rectangleSound = EZ.addSound("sandstorm.wav");
EZSound hatSound = EZ.addSound("airhorn.wav");

while (true) { // Assuming while loop even necessary
    int clickX = EZInteraction.getXMouse();
    int clickY = EZInteraction.getYMouse();

    if(EZInteraction.wasMouseLeftButtonReleased()) {

        if(hatPicture.isPointInElement(clickX, ClickY)) {

            selectedImage = hatPicture;
            hatSound.play();

        } else if(rectanglePicture.isPointInElement(clickX, ClickY)) {

            selectedImage = rectanglePicture;
            rectangleSound.play();

        } else {

            EZ.addImage("hat.png", clickX, clickY);
            EZ.refreshScreen();

        }

    } // End if

} // End loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM