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

2022/4/19

how do i start and stop a backgroundsong?

Jip_maris Jip_maris

2022/4/19

#
I would like to know how i could start a backgroundsong if I get into the world and how to stop it when i press enter, could someone help me? I am a beginner in greenfoot, so I don't know how everything works yet. this is the code for the world, but the music keeps going:
import greenfoot.*;

public class Gehaald extends World
{
    //(achtergrondmuziek toevoegen)
    GreenfootSound backgroundMusic = new GreenfootSound("AcousticShuffle.wav");
    //(manier toevoegen om naar het beginscherm terug te komen + grootte van de wereld)
    public Gehaald()
    {    
        //(grootte van de wereld + toevoegen van muziek)
        super(1000, 700, 1); 
        backgroundMusic.playLoop();
    }

    public void act()
    {

        //(als er op enter wordt gedrukt, begin je weer bij het startmenu)
        if (Greenfoot.isKeyDown("enter"))
        {
            Greenfoot.setWorld(new Startmenu());
        }
    }
    //(muziek laten stoppen als je uit het level gaat)
    public void stopped()
    {
        backgroundMusic.setVolume(0);
    }
}
danpost danpost

2022/4/22

#
You would use:
backgroundMusic.stop();
// or
backgroundMusic.pause();
// and
backgroundMusic.play();
// or
backgroundMusic.playLoop(); // of which you already use on line 12
These methods and more can be found here.
You need to login to post a reply.