Playing random sounds

For questions about using Classic.

Post » Wed Jan 07, 2009 4:37 am

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?
B
3
S
1
G
3
Posts: 105
Reputation: 1,312

Post » Wed Jan 07, 2009 5:04 am

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
B
2
S
2
G
5
Posts: 293
Reputation: 2,236

Post » Wed Jan 07, 2009 5:26 am

Thanks vinny, that did the trick. :)
B
3
S
1
G
3
Posts: 105
Reputation: 1,312

Post » Wed Jan 07, 2009 6:33 am

a cleaner 1 event way of doing it.

name your sounds richochet 1 to 9

-set Xaudio to load ("richochet"+str(random(9))+".wav")
B
25
S
3
G
6
Posts: 1,197
Reputation: 5,620

Post » Wed Jan 07, 2009 8:47 am

Thanks Davioware, that's what I was really looking for.
B
3
S
1
G
3
Posts: 105
Reputation: 1,312

Post » Wed Jan 07, 2009 4:13 pm

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"
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

Post » Wed Jan 07, 2009 5:20 pm

oh yea it slipped my mind that random(n) also returns zero. thanks for that & operator tip ash :)
didnt know about it.
B
25
S
3
G
6
Posts: 1,197
Reputation: 5,620

Post » Wed Jan 07, 2009 5:34 pm

There's that, and more, in the Expressions wiki page!
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 1 guest