Java “的代码”;如果鼠标在图片上释放,请选择图片。再次释放鼠标,然后放置图片“;

Java “的代码”;如果鼠标在图片上释放,请选择图片。再次释放鼠标,然后放置图片“;,java,if-statement,Java,If Statement,我需要写一个代码,在屏幕上放置一张图片,点击图片后播放声音,点击之后的任何地方都会放置标签。我已经完成了图片和声音部分,但我在放置标签时遇到了困难。我目前正在尝试编写一个命令if(EZInteraction.wasmouseleftbuttonrenrelease()&&hatsound.play()){。但是如果我添加括号并将代码更改为if(EZInteraction.wasmouseleftbuttonrenrelease())&&&hatsound.play(),它将不起作用{,它将显示令

我需要写一个代码,在屏幕上放置一张图片,点击图片后播放声音,点击之后的任何地方都会放置标签。我已经完成了图片和声音部分,但我在放置标签时遇到了困难。我目前正在尝试编写一个命令
if(EZInteraction.wasmouseleftbuttonrenrelease()&&hatsound.play()){
。但是如果我添加括号并将代码更改为
if(EZInteraction.wasmouseleftbuttonrenrelease())&&&hatsound.play(),它将不起作用{
,它将显示令牌错误&&invalid OnlySynchronized。请帮助。这是我第一个学期学习CS。我的声音和放置标签部分的代码是

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 (EZInteraction.wasMouseLeftButtonReleased()) && (hatsound.play()) { //if left mouse released and hat sound plays then
            EZ.addImage("hat.png", clickX, clickY); //hat.png will be placed
        }

您正在尝试确定声音是否必须播放?我不认为
hatsound.play()
返回一个
布尔值
。您可以通过以下方式引入布尔变量来执行此操作

int clickNum = 0;
boolean hatSoundPlay = false;
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
            if (!hatSoundPlay) {
                hatsound.play(); //then hatsound will play
                hatSoundPlay = true;
            } else {
                EZ.addImage("hat.png", clickX, clickY); //hat.png will be placed
                hatSoundPlay = false;
            }
        }
    } 
}

不过,最好的方法是查看播放声音的类的引用,并找出哪些方法和变量。您的
hatsound
变量的类型是什么?

问题不在于声音,而在于将图片放在声音播放之后。我希望它在单击图片后播放声音,然后在下次发布时播放左键单击,放置我刚才单击的图片。这就是我需要帮助的地方。我编辑了代码。它使用
hatSoundPlay
boolean作为开关。如果为false(第一次单击)它播放声音并将变量设置为true。如果再次单击,则显示图像并将变量设置为false。然后再次开始…谢谢您的帮助,但您能否告诉我如何将图片放置在其他位置?因为如果我再次单击原始图片,addImage将仅添加hat.png。它不会将图片放置在其他位置,除非在原始贴纸上。