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

2013/5/19

How to program music to turn on an off

arinb arinb

2013/5/19

#
Hey guys, I am having problems trying to turn on the music in my world after having just turned it off... The part I don't know how to code it to check whether the music is on or off yet in my if statement....
public class BalloonWorld extends World
{
    MainMenu mainMenu = new MainMenu();
    private GreenfootSound bgMusic = new GreenfootSound("OldFriends.wav");
    /**
     * Constructor for objects of class MainMenu.
     * 
     */
    public BalloonWorld()
    {    
        // Create a new world with 800x600 cells with a cell size of 1x1 pixels.
        super(800, 600, 1);
        background();
        bgMusic.play();
        GreenfootSound.isPlaying(true);
    }

    public void background() 
    {
        setBackground("background.jpg");
    }

    public void act() 
    {
        options(); // gives the image with turning on off sound instructions
        instructions(); //image with instructions is put up
        start(); // changes to the actual game world
        back(); //takes user back to the main menu
    }
    

public void options()
    {
        if (Greenfoot.isKeyDown("o"))
        {
            addObject(new Options(), 400, 300); 
        }
        if (Greenfoot.isKeyDown("f"))
        {
            bgMusic.stop();
        }
        if (Greenfoot.isKeyDown("n") && (GreenfootSound.isPlaying(false)))
        {
                bgMusic.play();
        } 
    }
danpost danpost

2013/5/19

#
The 'isPlaying' method in the GreenfootSound class is not being used properly. First, you cannot check to see if a Class (GreenfootSound) is playing. The method must be executed on a GreenfootSound object (bgMusic). Second, the method call has no arguments ('false' should not be inside the parens of the method call). To negate the value returned from the call, use an exclamation point symbol ('!').
if (Greenfoot.isKeyDown("n") && !bgMusic.isPlaying())
You probably have another problem with the multiple creations of Option objects when the "o" key is pressed. Change that check to the following:
if (Greenfoot.isKeyDown("o") && getObjects(Option.class).isEmpty())
danpost danpost

2013/5/19

#
Also, remove line 15 of your BalloonWorld class ('GreenfootSound.isPlaying(true);').
Gevater_Tod4711 Gevater_Tod4711

2013/5/19

#
You have to use GreenfootSound.isPlaying() but different to what you are trying. First the method isPlaying() is non-static which means that you have to use an instance of the class GreenfootSound to execute this method. The second thing is that the method has no parameter, which means that isPlaying() is the method call instead of isPlaying(true). In your case you will have to use it like this:
//in your constructor you don't even need this method. So you better delete it.

//in your options method;
//...
if (Greenfoot.isKeyDown("n") && !bgMusic.isPlaying()) {
    bgMusic.play();
}
In this example bgMusic is the instance of the class GreenfootSound. You call the method for this object not for the class like you tried it first. The method returns either true or false. Using the oparator '!' you can change a boolean value from true to false and the other way arround in a if statement (also in while, for, ...). So this code is checking whether: The key "n" is down and the sound is NOT playing.
arinb arinb

2013/5/19

#
Oh! I see, I just tried it out on my game and it works now! THANK YOU
xxPythonslayeRxx xxPythonslayeRxx

2014/9/8

#
thank u ^bread^
You need to login to post a reply.