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

2020/5/20

My music button doesn't stop

LukaMoon LukaMoon

2020/5/20

#
I made a actor class for a music class where when you click on it, it turns off the music. However, if I leave the music on, go through menus and return back to the world where the actor class was, the music starts again over top of the music that's currently playing. The music also runs when the game isnt even running
private GreenfootSound backgroundMusic = new GreenfootSound("bubblegumkk.mp3");
    private boolean isMuted;
    public Music()
    {
        backgroundMusic.playLoop();
    }

    public void act() 
    {
        {
            if(Greenfoot.mouseClicked(this))
            {
                isMuted = !isMuted;
                updateState();
            }
        }

    }

    private void updateState()
    {
        if(isMuted)
        {
            setImage("MusicOff.png");
            backgroundMusic.pause();
        }
        else
        {
            setImage("MusicOn.png");
            backgroundMusic.play();
        }
    }

    public void stopped()
    {
        backgroundMusic.setVolume(0);  //change bg to what you declared the file as
    }

    public void started()
    {
        backgroundMusic.setVolume(30);    //change bg to what you declared the file as
    }
danpost danpost

2020/5/20

#
LukaMoon wrote...
I made a actor class for a music class where when you click on it, it turns off the music. However, if I leave the music on, go through menus and return back to the world where the actor class was, the music starts again over top of the music that's currently playing.
That is because you create a new Music instance, which, in turn, has its own GreenfootSound instance of the music. So, you get two instances playing at once.
The music also runs when the game isnt even running
The started and stopped methods do not work automatically from an Actor subclass -- only from a World subclass. You may want to check out my BGMusic Actor Class Demo scenario.
danpost danpost

2020/5/21

#
You would only need an unmodified copy of the BGMusic class with a copy of the BGMusic.png image file in your images folder and set as the default image for instances of the class. Then anywhere in your project, you will be able to control the background music within your project by just creating an instance of the class; and by adding an instance into any world, the user will be able to toggle the music on and off.
LukaMoon LukaMoon

2020/5/21

#
danpost wrote...
You would only need an unmodified copy of the BGMusic class with a copy of the BGMusic.png image file in your images folder and set as the default image for instances of the class. Then anywhere in your project, you will be able to control the background music within your project by just creating an instance of the class; and by adding an instance into any world, the user will be able to toggle the music on and off.
Where do I put my music?
danpost danpost

2020/5/21

#
LukaMoon wrote...
Where do I put my music?
Put filename as parameter in BGMusic constructor call:
BGMusic bgm = new BGMusic("backgroundMusic.wav");
LukaMoon LukaMoon

2020/5/21

#
danpost wrote...
LukaMoon wrote...
Where do I put my music?
Put filename as parameter in BGMusic constructor call:
BGMusic bgm = new BGMusic("backgroundMusic.wav");
Thank you so much! Just what I needed
You need to login to post a reply.