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

2013/4/23

Music Help

SWAG SWAG

2013/4/23

#
How can I change my background music when I change from world to another? What would the code be?
Well, if this is following through on your teleporting question I answered, change the teleport method I made to something like this (note, i haven't used sounds yet, but I got my information from the greenfoot api: http://www.greenfoot.org/files/javadoc/):
private GreenfootSound song;
...

public /*World name*/(GreenfootSound s //...)
{
    ...
    song = s;
    song.play(); //or song.playLoop() if you want to go on continously
}

public void teleport()
{
   song.stop();
   //everything from your last question
}
SWAG SWAG

2013/4/23

#
I change worlds in my actor class and my music is playing in the first world. I want the music to change in the second world.
did you make sure to stop the song before switching worlds?
SWAG SWAG

2013/4/23

#
FlyingRabidUnicornPig wrote...
did you make sure to stop the song before switching worlds?
The music stop when the character dies, but I don't know how to make it stop when is switch worlds.
could I see your character die code?
SWAG SWAG

2013/4/23

#
I don't have any code for it, my music in my world it set to stop when game stops and my game stops when character dies.
alright. That's what I thought. Well, I haven't personally used GreenfootSound, but it should stop when you tell it to stop... Here's another question: Is your GreenfootSound object an instance variable (one of the private variables at the top of the class) in your world class?
SWAG SWAG

2013/4/23

#
yes it is a private variable at the top.
alright, so before you switch, you need to call the method stop somewhere. I'm guessing if you're using your teleport method as well, you should add this to your world:
public GreenfootSound getSound()
{
    return mySound; //Or whatever you called it
}
Then in your teleport code in the player:
public void teleport()
{
    getWorld().getSound().stop();
    ...
}
SWAG SWAG

2013/4/24

#
FlyingRabidUnicornPig wrote...
alright, so before you switch, you need to call the method stop somewhere. I'm guessing if you're using your teleport method as well, you should add this to your world:
public GreenfootSound getSound()
{
    return mySound; //Or whatever you called it
}
Then in your teleport code in the player:
public void teleport()
{
    getWorld().getSound().stop();
    ...
}
I get an error cannot find symbol - method getSound()
oops, gotta cast your world. Pretend your world is named: "GreenfootWorld"
//Change
getWorld().getSound().stop();

//to
((GreenfootWorld)getWorld()).getSound().stop();
The reason you get an error is because the normal World does not have a method called getSound(). You created it in GreenfootWorld. Therefore, you must cast getWorld() (which is of World) to GreenfootWorld with the code above
SWAG SWAG

2013/4/24

#
Now the sound stops but the second level sound doesn't start unless I pause the game and run it again
put an act() method in your world, and do this:
public void act()
{
    if (!songName.isPlaying())
        songName.play();
    ...
}
and then take out the line in the constructor that says play.
You need to login to post a reply.