PDA

View Full Version : Random background music



maruda
May 3rd, 2003, 07:34 AM
how to write a code that play background music randomly?

i write these in the frame
bgmusic = new Sound();
bgmusic.loadSound("1.mp3", true);


in the movie
there are buttons with action
on (release) {
bgmusic.loadSound("1.mp3", true);
}

//there are some buttons with 1, 2, ....mp3 respectively, also null for stop..

then how to make it play background music randomly within 1, 2, 3.mp3?

kode
May 3rd, 2003, 07:36 AM
bgmusic.loadSound(Math.ceil(Math.random()*3)+".mp3", true);
=)

maruda
May 3rd, 2003, 11:02 PM
thank you very much

kode
May 3rd, 2003, 11:17 PM
no problem. ;)

brian monkey
May 5th, 2003, 03:31 PM
Can you explain to me what Math.ceil means?

thanks
-brian2

kode
May 5th, 2003, 06:49 PM
Returns the closest integer that is greater than or equal to the number returned by Math.random()*3.

Examples:
Math.random()*3 returns 0.254530019871891, Math.ceil rounds it to 1.
Math.random()*3 returns 1.808154902886599, Math.ceil rounds it to 2.
Math.random()*3 returns 2.119892139919164, Math.ceil rounds it to 3.

Got it? =)