This site requires JavaScript, please enable it in your browser!
Greenfoot back
Zakuza
Zakuza wrote ...

2015/4/7

Setting volume level.

Zakuza Zakuza

2015/4/7

#
Trying to lower volume in my scenarios World. But it does not work. HELP. My settings OBJECT/CLASS (ACTOR).
import greenfoot.*;

public class Settings extends Actor
{  
    static int leftOffset = -50;
    
    static int leftSpawn = -50;
    
    static int rightOffset = 950;
    
    static int rightSpawn = 950;
    
    static int volume = 10;
    
    public void act()
    {
        volume();
    }
    
    public void volume()
    {
        if (volume == 10)
        {
            if (Greenfoot.isKeyDown("-"))
            {
                Meniu.meniu.setVolume(90);
            }
        }
        else if (volume == 9)
        {
            if (Greenfoot.isKeyDown("-"))
            {
                Meniu.meniu.setVolume(80);
            }
        }
        else if (volume == 8)
        {
            if (Greenfoot.isKeyDown("-"))
            {
                Meniu.meniu.setVolume(70);
            }
        }
        else if (volume == 7)
        {
            if (Greenfoot.isKeyDown("-"))
            {
                Meniu.meniu.setVolume(60);
            }
        }
        volume--;
    }
}
The world where these setting should work:
import greenfoot.*;

public class Meniu extends World
{
    public Meniu()
    { 
        super(900, 600, 1, false);
        prepare();
    }
    
    public void prepare()
    {
        Settings settings = new Settings();
        addObject(settings, -200, -200);
    }
    
    static GreenfootSound meniu = new GreenfootSound("Meniu.mp3");
    
    public void act() 
    {
        meniu.play();
        if (Greenfoot.isKeyDown("enter"))
        {
            meniu.stop();
            Greenfoot.setWorld(new GameWorld1());
        }
    }
}
I think I am doing something wrong with this part :
        if (volume == 10)
        {
            if (Greenfoot.isKeyDown("-"))
            {
                Meniu.meniu.setVolume(90);
            }
        }
specifically
 Meniu.meniu.setVolume(90);
Oh, and also I think I add object wrong:
        Settings settings = new Settings();
        addObject(settings, -200, -200);
x and y are -200 so that i wouldn't see the green foot on my screen. If my "addobject" and "if statement" are good, then my guess would be that my song/music volume is not decreasing because act is always acting it. Anyway I'm not good at programming, just started, it looks fun but I'm inexperienced.
danpost danpost

2015/4/7

#
Line 50 is the killer. It only takes fraction of a second for the value of 'volume' to become negative. As such, none of your 'if' blocks will execute because you check for values of between 10 and 7. You can simplify things if the value of 'volume' was the same as the settings:
public void volume()
{
    if (Greenfoot.isKeyDown("-") && volume > 0)
    {
        volume--;
        Meniu.meniu.setVolume(volume);
    }
    if (Greenfoot.isKeyDown("+") && volume < 100)
    {
        volume++;
        Meniu.meniu.setVolume(volume);
    }
}
Zakuza Zakuza

2015/4/7

#
Changed, hmm still seem to not work. well ill figure something out in time.
danpost danpost

2015/4/7

#
I suggest replacing lines 5 through 13 of the Settings class with the following:
private int volume = 100;
and deal with any issues that you may have from there.
Zakuza Zakuza

2015/4/8

#
Well i did that from the start because you said that "You can simplify things if the value of 'volume' was the same as the settings" so that much was clear. And guess what, I changed buttons and the code is working. you see i was using keypads + and - . and then i tried the top ones, it worked but not both, So simply i changed buttons. Thanks for help.
You need to login to post a reply.