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

2021/6/14

How add background music and stop when world changes

Gh0sT Gh0sT

2021/6/14

#
Hi, everyone! Well, i'm creating a game with several musics and scenarios, and i would like to know how i add musics in my worlds, for example: menu world. And stop the menu music when the game start or when credits button is pressed and the world changes to credit screen. I have a problem with that. Because i put the songs in the buttons of each scenario, so... in worlds with two or more button, when i press a button differently that the button with the method to play and stop the song, the music don't stop, and when a press to back to the menu, start two songs. Well, as you all can see, i need some help.
public class Menu extends World
{

//Here is my menu code, without methods to play songs

    GifImage background = new GifImage("GifMenuOriginal.gif");
      
    public void act()
    { 
        setBackground( background.getCurrentImage() );
         
    }
    public Menu() //Define as especificações de dimensão do mundo "Menu".
    {    
      super(672, 312, 1); 
      preparar();
      
    }
    
    public void preparar() //Realiza as ações da classe, como add objetos.
    {
     addObject(new CapaMenu(),544,156); //Adiciona a capa de "Menu"
     addObject(new Start(),544,155); //Adiciona o botão de "Start"
     addObject(new Creditos(),544,230); //Adiciona o botão de "Creditos"
    }    
}
public class Start extends Botao //Inicia o jogo
{

//Here is my start button code, i put the method GreenfootSound here, but the problem is: when pressed other button, like credits, sound don't stop

    GreenfootSound mainMusic = new GreenfootSound("hometheme.mp3");
   
    public void act() 
    {
       checarMouse();
       checkS();
   
    }
   public void checkS() 
    {
       
        if(Greenfoot.mouseClicked(this)){
        checarMouse();   
        checkClick(new Fase1());
        mainMusic.stop();
    }
    else
    {
       mainMusic.play();
    }
   }
}
Gbasire Gbasire

2021/6/14

#
usually, the best thing to do is to do something like this :
GreenfootSound mainMusic = new GreenfootSound("hometheme.mp3");

public void act()
{
    if(Greenfoot.mouseClicked(this))
    {
        //stop the music
        mainMusic.stop();
    }
    //check if the music isn't being played
    else if(!mainMusic.isPlaying())
    {
        //play the music
        mainMusic.play();
    }
}
Gabe1098 Gabe1098

2021/6/15

#
to make the whole thing stop then you use this
Greenfoot.stop();
Gabe1098 Gabe1098

2021/6/15

#
oh wait you meant the music I thought you meant the whole project
You need to login to post a reply.