【问题标题】:Volume control in GreenfootGreenfoot 中的音量控制
【发布时间】:2018-04-18 03:36:13
【问题描述】:

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

【问题讨论】:

    标签: java greenfoot


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      相关资源
      最近更新 更多