我正在尝试创建一个简单的绿色脚mp3播放器,并使用按钮来控制音量。我有一些代码,我认为应该可以工作,但它不是。我不确定问题出在哪里。我试着增加音量,当向上按钮被按下时,音量减少1,当向下按钮被按下时,我对编程相当陌生,所以任何帮助都会很大。谢谢!
public class Play extends Actor
{
public GreenfootSound sound = new GreenfootSound("AdventureOfaLifetime.mp3");
private boolean off = true;
int currentVolume = sound.getVolume();
Up volumeUp;
Down volumeDown;
/**
* Act - do whatever the Play wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (Greenfoot.mouseClicked(this) && off)
{
setImage("Pause.png");
sound.play();
off = false;
}
else if(Greenfoot.mouseClicked(this) && !off)
{
setImage("Play.png");
sound.pause();
off = true;
}
if(volumeUp.clicked)
{
if(currentVolume < 100)
{
currentVolume = currentVolume + 1;
sound.setVolume(currentVolume);
}
}
if(volumeDown.clicked)
{
if(currentVolume > 0)
{
currentVolume = currentVolume - 1;
sound.setVolume(currentVolume);
}
}
}
public class Down extends Play
{
static boolean clicked = false;
/**
* Act - do whatever the Down wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (Greenfoot.mouseClicked(this))
{
clicked = true;
}
else
{
clicked = false;
}
}
public class Up extends Play
{
static boolean clicked = false;
/**
* Act - do whatever the Up wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (Greenfoot.mouseClicked(this))
{
clicked = true;
}
else
{
clicked = false;
}
} 发布于 2018-04-18 11:52:13
我有点事要做。我只是将currentVolume设置为50,然后将if语句更改为currentVolume += 1;和currentVolume -= 1;。这可能不是最好的解决方案,但至少它是有效的。
https://stackoverflow.com/questions/49890760
复制相似问题