0

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

1 回答 1

0

我有事要干。我只是设置currentVolume为 50 然后将我的 if 语句更改为currentVolume += 1;and currentVolume -= 1;。可能不是最好的解决方案,但至少它可以工作。

于 2018-04-18T03:52:12.850 回答