Greenfoot中的音量控制

时间:2018-04-18 03:36:14

标签: java greenfoot

我正在尝试用绿脚创建一个简单的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;
    }  
}    

1 个答案:

答案 0 :(得分:0)

我得到了一些工作。我只是将currentVolume设置为50,然后将我的if语句更改为currentVolume += 1;currentVolume -= 1;。可能不是最好的解决方案,但至少它的工作。