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

3 days ago

how to stop music after changing a few times of world ?

rojina rojina

3 days ago

#
hello, i start a music in my first world, but then i change world a few times. after i arrive a certain world i want the music to stop, i want to change it how do i do that ? sorry if my english is bad, its not my first language lol
danpost danpost

3 days ago

#
rojina wrote...
start a music in my first world, but then i change world a few times. after i arrive a certain world i want the music to stop, i want to change it how do i do that ?
Use the following ( I named first world MyWorld and sound file intro.mp3 ):
1
public static GreenfootSound bgSound;
and in constructor ( public MyWorld() ):
1
2
bgSound = new GreenfootSound("intro.mp3");
bgSound.play();
In any class in your project, you can then use:
1
MyWorld.bgSound.stop();
To change it, replace what the field references::
1
2
MyWorld.bgSound = new GreenfootSound("replacement.mp3");
MyWorld.bgSound.play();
danpost danpost

3 days ago

#
You could go as far as putting the following the the MyWorld class:
1
2
3
4
5
6
7
public static void setBackgroundSound(String name) {
    if (bgSound != null) bgSound.stop();
    bgSound = null;
    if (name == null) return;
    bgSound = new GreenfootSound(name);
    bgSound.play();
}
In the constructor, you could then just have:
1
setBackgroundSound("intro.mp3");
Then, to change it from anywhere:
1
MyWorld.setBackgroundSound("replacement.mp3);
You can also stop and discard the background sound altogether with:
1
myWorld.setBackgroundSound(null);
rojina rojina

yesterday

#
thank you for your answer! i tried the second method you gave me and it works ! you are the best
You need to login to post a reply.