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

2018/5/21

How to change the music with a button?

Recorsi Recorsi

2018/5/21

#
Hello, i have some music running in the background. It randomly picks one of 12 different soundfiles at the start of the game. The code for the music and the game start (i have a title screen):
static GreenfootSound BG;
public static boolean enterPressed = false;
static GreenfootSound GameMusic;
public void Music()
    {
       setBackground(Startscreen.getCurrentImage()); 
       if (!enterPressed && Greenfoot.isKeyDown("enter"))
       {
          enterPressed = true;
          if (GameMusic != null)
          {
             GameMusic.stop();
             GameMusic = null;
          }
          else
          {
             GameMusic = new GreenfootSound("Musik"+(Greenfoot.getRandomNumber(11)+1)+".mp3");
             GameMusic.playLoop();
             GameMusic.setVolume(90);
             Greenfoot.setWorld(new Spielfeld());
             BG.stop();
          }
        }
       else if (!Greenfoot.isKeyDown("enter"))enterPressed = false; 
    }
Now to the issue: I wanted to make a method so that you can change the music by pressing "c", which looks like this:
static GreenfootSound newGameMusic; //I created newGameMusic because i couldnt figure out how to change GameMusic after it picked a songfile
private boolean cDown;
public void changeMusic()
    {
        if (cDown != Greenfoot.isKeyDown("c")) 
        {
           cDown =! cDown; 
           if (cDown) 
           {
                  Startscreen.GameMusic.stop();
                  newGameMusic = new GreenfootSound("Musik"+(Greenfoot.getRandomNumber(11)+1)+".mp3");
                  newGameMusic.stop();
                  newGameMusic.playLoop();
           }
        }                      
    }
The problem with this is that i can only change the music once. Is it possible to change the boolean after it picked the song? Or are there any other solutions? Sorry for the long question and thanks for your help :).
danpost danpost

2018/5/21

#
Recorsi wrote...
The problem with this is that i can only change the music once. Is it possible to change the boolean after it picked the song? Or are there any other solutions?
You should be able to change GameMusic. Add a public access modifier to the GameMusic initialization line.
Recorsi Recorsi

2018/5/22

#
danpost wrote...
You should be able to change GameMusic. Add a public access modifier to the GameMusic initialization line.
Like this?
public static GreenfootSound GameMusic;
How do i change it then? Just stopping and starting it again doesn't work
danpost danpost

2018/5/22

#
Recorsi wrote...
How do i change it then? Just stopping and starting it again doesn't work
StartScreen.GameMusic.stop();
StartScreen.GameMusic = new GreenfootSound("MUsik"+(Greenfoot.getRandomNumber(12)+1)+".mp3");
StartScreen.GameMusic.setValume(90);
StartScreen.GameMusic.playLoop();
Recorsi Recorsi

2018/5/22

#
Thanks for your help
You need to login to post a reply.