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

2021/2/18

Menu music

BogdanMicu BogdanMicu

2021/2/18

#
I'm adding music to the main menu and I want it to play through the StartScreen, LevelSelector and Guide without stopping. I noticed that if you simply playLoop(), it doesn't stop when you switch the world and that's exactly the effect I wanted, but every time I come back to the world which initially played the song, a new one is initialised and the song starts for a second time over the first instance. How could I simply add the menu music without it starting over again and again and be able to remove it later? This is how I tried to implement it:
if(!bgMusic.isPlaying()){bgMusic.setVolume(50);bgMusic.playLoop();}
It should be removed only when a level is chosen in the LevelSelector.
danpost danpost

2021/2/18

#
BogdanMicu wrote...
I'm adding music to the main menu and I want it to play through the StartScreen, LevelSelector and Guide without stopping. I noticed that if you simply playLoop(), it doesn't stop when you switch the world and that's exactly the effect I wanted, but every time I come back to the world which initially played the song, a new one is initialised and the song starts for a second time over the first instance. How could I simply add the menu music without it starting over again and again and be able to remove it later?
public static GreenfootSound bgMusic;

public void started()
{
    if (bgMusic == null)
    {
        bgMusic = new GreenfootSound("background.mp3");
        bgMusic.setVolume(50);
        bgMusic.playLoop();
    }
}
AnJoMorto AnJoMorto

2021/2/18

#
danpost wrote...
public static GreenfootSound bgMusic;

public void started()
{
    if (bgMusic == null)
    {
        bgMusic = new GreenfootSound("background.mp3");
        bgMusic.setVolume(50);
        bgMusic.playLoop();
    }
}
Continuing this thread, how can I stop the song then, once I changed to a world where I don't want the music to play there?
BogdanMicu BogdanMicu

2021/2/18

#
danpost wrote...
BogdanMicu wrote...
I'm adding music to the main menu and I want it to play through the StartScreen, LevelSelector and Guide without stopping. I noticed that if you simply playLoop(), it doesn't stop when you switch the world and that's exactly the effect I wanted, but every time I come back to the world which initially played the song, a new one is initialised and the song starts for a second time over the first instance. How could I simply add the menu music without it starting over again and again and be able to remove it later?
public static GreenfootSound bgMusic;

public void started()
{
    if (bgMusic == null)
    {
        bgMusic = new GreenfootSound("background.mp3");
        bgMusic.setVolume(50);
        bgMusic.playLoop();
    }
}
That worked, thanks! The LevelSelector has a button for each level and when that button is pressed the menu music should stop, so I tried this with ((StartScreen)getWorld()).bgMusic.pause(); but it didn't work because there's no reference of the actual button in the StartScreen so it can't access it. How could I remove it so the level music can start?
danpost danpost

2021/2/18

#
AnJoMorto wrote...
how can I stop the song then, once I changed to a world where I don't want the music to play there?
I realize this will not work either (I do not think); but,
StartScreen.bgMusic.stop();
"StartScreen" here is presumed to be the class where my above code was placed.
danpost danpost

2021/2/18

#
As a side note, background music is something your World object should maintain. If you put my code in the class of a level button, then it is not where it should be.
BogdanMicu BogdanMicu

2021/2/18

#
danpost wrote...
As a side note, background music is something your World object should maintain. If you put my code in the class of a level button, then it is not where it should be.
Yeah, but it remains playing when I switch to the LevelSelector and should be stopped after a level is selected. Is it possible?
danpost danpost

2021/2/18

#
BogdanMicu wrote...
it remains playing when I switch to the LevelSelector and should be stopped after a level is selected. Is it possible?
What class did you place my code above in?
AnJoMorto AnJoMorto

2021/2/18

#
danpost wrote...
I realize this will not work either (I do not think); but,
StartScreen.bgMusic.stop();
"StartScreen" here is presumed to be the class where my above code was placed.
It's working perfectly. Thank you danpost for all the time invested helping people here.
BogdanMicu BogdanMicu

2021/2/18

#
danpost wrote...
BogdanMicu wrote...
it remains playing when I switch to the LevelSelector and should be stopped after a level is selected. Is it possible?
What class did you place my code above in?
StartScreen
danpost danpost

2021/2/18

#
BogdanMicu wrote...
StartScreen
Then my "stop" line should work. You can use it anywhere in your project. Two possible placements are (1) where level button is found to be clicked; or (2) in your level world constructors.
BogdanMicu BogdanMicu

2021/2/18

#
It was easier than I was expecting, but I gotta say it. You're a god man. Dunno where you learned all this stuff from or where does your patience come from, but you helped me with a lot of things for my project and I feel like I gotta state my appreciation somehow. I almost finished it now. All I've got left to do is debug the movement for the moving platforms and it's done. Platforms moving towards the character, opposite of the direction that he is moving, teleport into him and freeze the game. For example, a vertical platform has no problem with the character moving on it when descending, but when ascending, I implemented a setLocation(getX(), getY()-1) which works fine if the character doesn't move. If he moves, the fall checker comes in play and the platform gets inside of him. Same thing happens when approaching from the side a horizontal platform. How do you reckon this could be solved? Here 's a more detailed insight on the problem.
BogdanMicu BogdanMicu

2021/2/19

#
danpost wrote...
BogdanMicu wrote...
StartScreen
Then my "stop" line should work. You can use it anywhere in your project. Two possible placements are (1) where level button is found to be clicked; or (2) in your level world constructors.
One more question. When returning to the main menu through the menu button, the music doesn't play anymore, even though the started() is used in act(). How could I make it play?
danpost danpost

2021/2/19

#
Switch my lines 9 and 10. Then call started(); from act.
BogdanMicu BogdanMicu

2021/2/19

#
Now it works! Thanks a lot! I feel like these things are pretty obvious and straightforward but it sometimes just doesn't cross my mind. I guess it comes with experience and you gotta actually understand how Java works too :) . Thanks again!
You need to login to post a reply.