This site requires JavaScript, please enable it in your browser!
Greenfoot back
rachpaguia@gmail.com
rachpaguia@gmail.com wrote ...

2017/3/17

New Bg Music

Hi does anyone know how to set a new background music? I have a bg music for my program with a game background , and im planning to put another one for my game over background
Super_Hippo Super_Hippo

2017/3/17

#
Show how you created your current background music.
public class bg extends World
{
    GreenfootSound backgroundMusic = new GreenfootSound("music.mp3");
public bg()
    {//this sets the background     
super(600, 400, 1);
setBackground("Final ICT BG.png");
backgroundMusic.playLoop();
        
GreenfootImage bg = new GreenfootImage("Final ICT BG.png");
bg.scale(getWidth() +35, getHeight() +30);
setBackground(bg);
prepare();
//if("enter".equals(Greenfoot.getKey()));
}
private int spawnTimer; 
public void act()
    { checkForSpawning();

    }
private void checkForSpawning()
// this action adds an actor after every particular time

    {
        spawnTimer = (spawnTimer+1)%500;
        if (spawnTimer%55 == 0) addObject(new A(), 150, 0);
        if (spawnTimer%40 == 0) addObject(new L(), 250, 0);
        if (spawnTimer%32 == 0) addObject(new H(), 60, 0);
        if(spawnTimer%87 == 0)addObject(new I(), 330, 0);
        if(spawnTimer%95 == 0)addObject(new O(), 520, 0);
        if(spawnTimer%68 == 0)addObject(new R(), 400, 0);
    } 
    
private void prepare()
//this prepares objects and adds them to the world before program begins
    {

    }

   public void gameOver()
{
    removeObjects(getObjects(Actor.class));
    GreenfootImage bg = new GreenfootImage("Go.png");
    bg.scale(getWidth(), getHeight());
    setBackground(bg);
    backgroundMusic.pause();
    GreenfootSound backgroundMusic = new GreenfootSound("go.mp3");
    Greenfoot.stop();
}
}
Here!! :)
Super_Hippo Super_Hippo

2017/3/17

#
I think you only have to start the new backgroundMusic after line 47.
danpost danpost

2017/3/17

#
You should probably use 'stop' instead of 'pause' on line 46. Then, remove the first word from line 47 (to have the new sound assigned to the field the old sound was assigned to.. You will then need a line to start the new sound after that.
public class bg extends World
{
    GreenfootSound backgroundMusic = new GreenfootSound("music.mp3");
public bg()
    {//this sets the background     
super(600, 400, 1);
setBackground("Final ICT BG.png");
backgroundMusic.playLoop();
        
GreenfootImage bg = new GreenfootImage("Final ICT BG.png");
bg.scale(getWidth() +35, getHeight() +30);
setBackground(bg);
prepare();
//if("enter".equals(Greenfoot.getKey()));
}
private int spawnTimer; 
public void act()
    { checkForSpawning();

    }
private void checkForSpawning()
// this action adds an actor after every particular time

    {
        spawnTimer = (spawnTimer+1)%500;
        if (spawnTimer%55 == 0) addObject(new A(), 150, 0);
        if (spawnTimer%40 == 0) addObject(new L(), 250, 0);
        if (spawnTimer%32 == 0) addObject(new H(), 60, 0);
        if(spawnTimer%87 == 0)addObject(new I(), 330, 0);
        if(spawnTimer%95 == 0)addObject(new O(), 520, 0);
        if(spawnTimer%68 == 0)addObject(new R(), 400, 0);
    } 
    
private void prepare()
//this prepares objects and adds them to the world before program begins
    {

    }

   public void gameOver()
{
    removeObjects(getObjects(Actor.class));
    GreenfootImage bg = new GreenfootImage("Go.png");
    bg.scale(getWidth(), getHeight());
    setBackground(bg);
    backgroundMusic.stop();
    backgroundMusic = new GreenfootSound("go.mp3");
    GreenfootSound backgroundMusic = new GreenfootSound("go.mp3");
    Greenfoot.stop();
}
}
Thanks! Did you mean something like this? It still isn't working. I actually think it's because of the Greenfoot.stop method but without this my actors will still be working even if it's ga,e over
Super_Hippo Super_Hippo

2017/3/17

#
Change line 48 to:
backgroundMusic.playLoop();
Thank you! Will it be possible to do this without the loop because the sound is 3 seconds long only and i only plan to play it once
Super_Hippo Super_Hippo

2017/3/17

#
Remove the 'Loop' then:
backgroundMusic.play();
Oh, i didn't know that was possible. thank you so much for your help!
You need to login to post a reply.