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

2017/10/15

How to add background music?

Reginold Reginold

2017/10/15

#
Anyone knows.? Please tell me
danpost danpost

2017/10/15

#
The simplest way is to use 'Greenfoot.playSound( /** string filename of music file */ );'. With that, however, you have no control over it -- it will play until completed (unless you close the project outright). Better is to create and keep a reference to a GreenfootSound object which you can control. You will need to copy the sound file(s) into the 'sounds' folder of the project.
Reginold Reginold

2017/10/15

#
But How to pause it when pause the game?
danpost danpost

2017/10/15

#
Reginold wrote...
But How to pause it when pause the game?
Depends on what code you are using for it to begin with. Show please.
Reginold Reginold

2017/10/16

#
Do you know how to pause background music when any actor die? For example - Bee eat by Frog, When frog eat, How to pause the background music and play game over music?
danpost danpost

2017/10/16

#
If only one frog in world, you can put something like the following in the world's act method:
if (getObjects(Frog.class).isEmpty()) bgMusic.pause();
BTW, my collections are to make it easier to find a scenario among all of mine (only meant for my scenarios -- sorry).
Reginold Reginold

2017/10/17

#
danpost wrote...
If only one frog in world, you can put something like the following in the world's act method:
if (getObjects(Frog.class).isEmpty()) bgMusic.pause();
BTW, my collections are to make it easier to find a scenario among all of mine (only meant for my scenarios -- sorry).
ThankYou But "getObjects" is make error
Reginold Reginold

2017/10/17

#
(This code in Actor Bee)
GreenfootSound music = new GreenfootSound("BackgroundMusic.mp3");
    public void backgroundMusic() 
    {
        music.playLoop();
        if (counter.getValue() >=300 ) 
        {
            music.pause();
        }
        Actor bee = getOneIntersectingObject(Bee.class);
        if (bee != null)
        {
            music.pause();
        }
    }
This is my code^
getOneIntersectingObject(Bee.class);
        if (bee != null)
        {
            music.pause();
        }
    }
But this code doesn't work^ Can you fix that please?
Reginold Reginold

2017/10/17

#
I need after bee die music pause!
Reginold Reginold

2017/10/17

#
Bumble Bee - This is that game. It doesn't stop, after bee die! Go to the link and fix it please
danpost danpost

2017/10/17

#
Add a boolean field to the class with the music:
private boolean musicStarted;
Then change:
music.playLoop();
to
if ( ! musicStarted)
{
    music.playLoop();
    musicStarted = true;
}
Now, you won't restart it every time the method is executed. You will also need this line in the Frog and Barrel classes:
((Bee)bee).music.pause();
Reginold Reginold

2017/10/17

#
Thank You so so so much It's working!!!!!!!!!!!!!!!!!
You need to login to post a reply.