Playing random sounds

This forum is currently in read-only mode.
From the Asset Store
This Student Workbook uses both Construct 3 & 2 encoding. It supplies client-side & php "back-end" encoding.
  • I'm making a top down shooter and it's going pretty well. However I have run into a problem: When bullets hit a wall, I want them to make a ricochet sound. I have 9 ricochet sounds that I want to use (ric1, ric2, ric3 etc.), and I want one of them to be randomly played when the bullet hits the wall. What is the easiest way of doing this?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • i haven't tried this myself but try doing this:

    give the bullet a variable and name it "sfx" or whatever you want and you can default the value to whichever number out of the 9 you will be using

    + on collision between bullet and wall (or when bullet overlaps wall, try both)

    • set variable "sfx" to a random number between 0 and 8 (or 1 and 9, whatever)

    + (subevent) variable "sfx" is equal to 0

    • play bulletSFX0.mp3
    • destroy bullet

    + (subevent) variable "sfx" is equal to 1

    • play bulletSFX1.mp3
    • destroy bullet

    etc ...

    taking into account the order of events, this works in my head; the main event should generate a value for the variable, "sfx" and with the condition for bullet collision/overlapping already met in the main event all the subevents have left to do is check the value stored in the variable

    just make sure that the "destroy bullet" action is the last one made as you will require the value stored in the variable to choose which sound file to play

  • Thanks vinny, that did the trick.

  • a cleaner 1 event way of doing it.

    name your sounds richochet 1 to 9

    -set Xaudio to load ("richochet"+str(random(9))+".wav")

  • Thanks Davioware, that's what I was really looking for.

  • random(9) returns 0-8, so unless your sounds are ric0, ric1, ric2 etc, you want to add 1 to that. Also, the & operator is better for combining strings and numbers in to strings. Try using the filename:

    "richochet" & (random(9)+1) & ".wav"

  • oh yea it slipped my mind that random(n) also returns zero. thanks for that & operator tip ash

    didnt know about it.

  • There's that, and more, in the Expressions wiki page!

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