我需要编写一个代码,在屏幕上放置一张图片,一旦点击图片就播放声音,然后点击之后点击的任何地方都会放置贴纸。我完成了图片和声音部分,但我很难贴上贴纸。我目前正在尝试编写命令if (EZInteraction.wasMouseLeftButtonReleased() && hatsound.play()) {
。但它不会起作用,如果我添加括号并将代码更改为if (EZInteraction.wasMouseLeftButtonReleased()) && (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
}
答案 0 :(得分:0)
你试图找出声音是否必须播放?我认为hatsound.play()
不会返回boolean
。你可以通过以下方式引入一个布尔变量来做到这一点。
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
变量的类型是什么?