randomize the music?

0 favourites
  • 9 posts
From the Asset Store
6 looping tracks to use in your games and projects. These tracks are in the style of the 1960s detective movie genre.
  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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


  • Thanks ArcadEd,

    This is how it looks now:

    <img src="http://4.bp.blogspot.com/-GMOGZNlQq84/Ucr2K_HZ9wI/AAAAAAAAABI/lJx7FQpkwL4/s1600/fffdddd.png" border="0" />

    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.)

  • 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.

  • Floor(random(6)) did the trick!

    Thanks for the tip.

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





    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.

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

  • best(easy) way is play by name

    and then


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

  • Thanks!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)