Playing random sound files with one cllickabe hotspot

Started by jamesreg, Sun 19/07/2009 19:44:15

Previous topic - Next topic

jamesreg

Hello all,

I want to have a hotspot on the screen and have the user be able to click it and then want a sound file to play but I wish to have several optional sound files to play and have ags pick at random which one will play when the player clicks that hotspot. Hope that made sense.

Im sure its very easy to code but I just got to the point of learning to make one sound file play lol.

Could someone be kind enough to possibly show me how I would achieve what I want.

Thank you in advance

GuyAwesome

You just need to use PlaySound and Random.
It's easiest if the sounds you want to play are numbered consecutively (sound1.mp3, sound2.mp3, sound3.ogg, etc) then you just need
Code: ags

PlaySound(4+Random(4));


Which'll select and play a random sound between 4 (4+0) and 8 (8+0). Or more generally: PlaySound([number of first sound] + Random([number of possible sounds]);
If the sounds aren't consecutive, just set an int to the random number and use a bunch of if ... elses
Code: ags

int ToPlay = Random(4);
if (ToPlay == 0) PlaySound(27);
else if (ToPlay == 1) PlaySound(56);
//... Ect



Quote
Im sure its very easy to code but I just got to the point of learning to make one sound file play lol.
Yeah, this is pretty easy stuff, even if you are just starting out. Get comfortable with what you do know before How I figure things out is to just read through the manual and actually try things out - even if you can't get it working, you can show what you've tried which (when you get to more complicated stuff) might give people a better idea exactly what you're after. At least you won't be posting questions that you can answer yourself less than 10 minutes later... :)

SMF spam blocked by CleanTalk