randomize the music?

Get help using Construct 2

Post » Wed Jun 26, 2013 2:42 pm

Hi.

I got about 3/4 of the way through the 'Generating Random Numbers' Tutorial before I gave up.

Is there a simple way of selecting a random music file to play at the start of each level?

I have 6 merry tracks.

many thanks.

B
13
S
1
Posts: 68
Reputation: 1,198

Post » Wed Jun 26, 2013 2:48 pm

Create a variable MUSICPLAY

On start of layout set MUSICPLAY to random(6)

You can use sub events off start of layout or a function to do the comparison:

If MUSICPLAY = 1, Play this music
IF MUSICPLAY = 2, play this music

Etc.
B
101
S
32
G
12
Posts: 1,549
Reputation: 21,993

Post » Wed Jun 26, 2013 3:15 pm

Thanks ArcadEd,

This is how it looks now:



But it's still not quite right. No music is playing. Can you see what I've done wrong?

(I set Music_Play as a global variable because I couldn't find the local variable button.)
B
13
S
1
Posts: 68
Reputation: 1,198

Post » Wed Jun 26, 2013 4:14 pm

Random gives you a float, not an integer. You need to use "floor(random(6))" and it will work.
In your particular case though, I would suggest you play sound from string, not from file, letting you construct the name of the music, something like Play "fid_"&MusicPlay, as all your music (except the first index) has a name of this type.
B
17
S
8
G
4
Posts: 473
Reputation: 6,221

Post » Wed Jun 26, 2013 4:46 pm

Floor(random(6)) did the trick!

Thanks for the tip.
B
13
S
1
Posts: 68
Reputation: 1,198

Post » Wed Jun 26, 2013 5:12 pm

You can make this a little easier if you name all the music similarly. For example, if you name the music:

Music0
Music1
Music2
Music3

You could play one of them randomly by using the "Play(By Name)" action:

Play "Music"&Floor(Random(4))

Of course, naming music similarly isn't always a viable the best option.

zatyka2013-06-26 17:13:00
B
64
S
19
G
8
Posts: 341
Reputation: 12,195

Post » Wed Jun 26, 2013 5:33 pm

Sorry, forgot to mention the float thing. Glad you got it working.

B
101
S
32
G
12
Posts: 1,549
Reputation: 21,993

Post » Wed Jun 26, 2013 6:17 pm

best(easy) way is play by name

and then

choose("audio-name","audio-name","...","...")

so you can leave names as is, and choose will pick random one

ImageImage
B
71
S
22
G
7
Posts: 827
Reputation: 10,177

Post » Sat Jun 29, 2013 9:29 pm

Thanks!
B
13
S
1
Posts: 68
Reputation: 1,198


Return to How do I....?

Who is online

Users browsing this forum: dop2000, NN81, work3, Yahoo [Bot] and 6 guests