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

2012/6/3

Adding background music?

1
2
Jesse_Orange Jesse_Orange

2012/6/3

#
I have sounds that play with acts but how do i add background music?
marcelo_japa marcelo_japa

2012/6/3

#
Use GreenfootSound.playLoop(). Like this: GreenfootSound backgroundMusic = new GreenfootSound("your sound file path"); backgroundMusic.playLoop();
Jesse_Orange Jesse_Orange

2012/6/3

#
Do I add that in the world as its own method or do I just put it in? Thanks though :)
danpost danpost

2012/6/3

#
It is probably better to put the first line in as a world instance variable and put the second one in the constructor of the world.
Jesse_Orange Jesse_Orange

2012/6/3

#
Can i have an example please? :) Thanks
danpost danpost

2012/6/3

#
Example code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import greenfoot.*;
 
public class MyWorld extends World
{
    GreenfootSound backgroundMusic = new GreenfootSound("musicFilename.mp3");
 
    public MyWorld()
    {
        super(600, 400, 1);
        backgroundMusic.playLoop();
        // etc.
    }
    // etc.
}
danpost danpost

2012/6/3

#
Breakdown:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// ***   begin class header  ***
// import statement(s)
import greenfoot.*;
 
// class declaration statement
public class MyWorld extends World
// ***  end class header  ***
 
// *** begin class code  ***
{
    // field declaration statement(s)
    GreenfootSound backgroundMusic = new GreenfootSound("musicFilename.mp3");
 
    // ***  begin instance object constructor(s)  ***
    public MyWorld()
    {
        super(600, 400, 1);
        backgroundMusic.playLoop();
        // etc.
    }
    // ***  end instance object constructor(s)  ***
 
    // ***  begin method(s), if any  ***
    //  etc.
    // *** end method(s), if any  ***
}
// ***  end class code  ***
Jesse_Orange Jesse_Orange

2012/6/3

#
"Unclosed string literal" what does this mean?
danpost danpost

2012/6/3

#
It means you used a double quote somewhere, but did not have a matching one.
Jesse_Orange Jesse_Orange

2012/6/5

#
Help! When the game stops how do i stop the background music? Its the frogger theme tune and it gets annoying as it never stops :(
tylers tylers

2012/6/5

#
add this into the world class:
1
2
3
4
5
6
7
8
public void stopped()
    {
      bg.setVolume(0);  //change bg to what you declared the file as
    }
    public void started()
    {
       bg.setVolume(100);    //change bg to what you declared the file as
    }
SPower SPower

2012/6/5

#
What tylers is saying will set the volume to 0 and to the maximum again, but to really pause it, do this:
1
2
3
4
5
6
7
8
9
public void stopped()
{
     bg.pause();
}
 
public void started()
{
    bg.playLoop();
}
This will pause the music, and let it play again when the world is started. I hope this helps.
Jesse_Orange Jesse_Orange

2012/6/5

#
if my file name is Frogger.mp3 would i write Frogger.mp3.code
SPower SPower

2012/6/5

#
No, just Frogger.mp3 Why would you add .code to it?
Jesse_Orange Jesse_Orange

2012/6/5

#
Oh no lol. I meant .code as in the next bit of code i was going to write :)
There are more replies on the next page.
1
2