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

2016/5/9

Background Music

ZacBuzzsaw ZacBuzzsaw

2016/5/9

#
I've been trying to create a game with background music that will a) only start when the "Run" button is pressed, b) play five different tracks, and c) prevent itself from playing the same track twice. The code I've written thus far is below. My question is this: how do I ensure that the music starts only if the user click the "Run" button? public void playMusic() { boolean song1Played, song2Played, song3Played, song4Played, song5Played; song1Played = false; song2Played = false; song3Played = false; song4Played = false; song5Played = false; GreenfootSound song1 = new GreenfootSound("sounds/runincircles.mp3"); GreenfootSound song2 = new GreenfootSound("sounds/atriumcomplex.mp3"); GreenfootSound song3 = new GreenfootSound("sounds/fliesonstrings.mp3"); GreenfootSound song4 = new GreenfootSound("sounds/cityonwater.mp3"); GreenfootSound song5 = new GreenfootSound("sounds/monkeynation.mp3"); int chooseSong = (int)(Math.random()*5); if(chooseSong == 0) { if(song1Played == false) { song1.play(); song1Played = true; } } else if(chooseSong == 1) { if(song2Played == false) { song2.play(); song2Played = true; } } else if(chooseSong == 2) { if(song3Played == false) { song3.play(); song3Played = true; } } else if(chooseSong == 3) { if(song4Played == false) { song4.play(); song4Played = true; } } else { song5.play(); song5Played = true; } }
SPower SPower

2016/5/9

#
You could call your method playMusic in your world's started method:
public void started()
{
    playMusic();
}
The problem with your current implementation is that you create a couple of booleans inside the method that needs to keep track of which music is playing, but because you create those inside the method, they will all be gone once the method ends. You need to declare those variables outside of the method, and you need to do the same for the GreenfootSound objects. Also, if you post code on here, please use the (without the spaces) tags, or click the 'code' button underneath the text box.
danpost danpost

2016/5/9

#
As well as declaring the GreenfootSound objects outside the method, it would be best to place them in an array. Being separate, you cannot make the one-to-one correspondence between the song number and the song playing that is needed. In an array, the number can represent the index number in the array (or one more than the number -- using zero for no selection). There is no need for any of the boolean fields at all as the index value.
// the array
private GreenfootSound[] songs =
{
    new GreenfootSound("sounds/runincircles.mp3"),
    new GreenfootSound("sounds/atriumcomplex.mp3"),
    new GreenfootSound("sounds/fliesonstrings.mp3"),
    new GreenfootSound("sounds/cityonwater.mp3"),
    new GreenfootSound("sounds/monkeynation.mp3")
};

// a more versatile playMusic method would be
private void playMusic(int songNumber)
{
    if (songPlaying > 0) songs[songPlaying-1].stop(); // stop any song that could already be playing
    songPlaying = songNumber; // select new song (or no song if zero)
    if (songPlaying == 0) return; // any song selected was cleared
    songs[songPlaying-1].play(); // new song selected and started
}

// the started method
public void started()
{
    playMusic(1+Greenfoot.getRandomNumber(songs.length));
}
ZacBuzzsaw ZacBuzzsaw

2016/5/12

#
I'm not quite sure where you're getting the songPlaying variable. Am I just missing something blatantly obvious? I tend to do that, unfortunately.
danpost danpost

2016/5/12

#
ZacBuzzsaw wrote...
I'm not quite sure where you're getting the songPlaying variable. Am I just missing something blatantly obvious? I tend to do that, unfortunately.
Sorry, it would be like your chooseSong field, renamed and declared outside the method so that its value is retained.
You need to login to post a reply.