[SOLVED] Random audio collisions with moving car. If !audio_is_playing...

Discussion in 'Programming' started by Crispywrists, Oct 10, 2016.

  1. Crispywrists

    Crispywrists Guest

    Hello!

    So I'm trying to make it so when my main character bumps into a moving car it plays one of 3 audio files I have made. I got it so the car stops when colliding with the main character and plays the audio but the audio played multiple times, layering infinitely to become a robotic mess of nothingness.

    So I checked the internet and came across:

    if !audio_is_playing(snd_1) { audio_play_sound(snd_1,1,0) }

    which works nicely, but I want it to play a random choice of audio, so I tried:

    if !audio_is_playing(snd_1) {audio_play_sound(choose(snd_outtheway1, snd_outtheway2,snd_outtheway3), 0, false); }

    But of course it's only checking for snd_1, and not snd_2, or snd_3 - I was wondering if there was a way to add snd_2 & snd_3 to the list of audio files to check whether they're playing or not....or if there is a much better way of coding this.

    Many thanks in advance.
     
  2. Bingdom

    Bingdom Googledom

    Joined:
    Jul 1, 2016
    Posts:
    1,678
    What you can do is
    Code:
    sound = audio_play_sound();
    if !audio_is_playing(sound) sound = audio_play_sound(choose(snd_outtheway1, snd_outtheway2,snd_outtheway3), 0, false);  
    Here is a quote from the manual that will explain much better than me. ;)
     
  3. Crispywrists

    Crispywrists Guest


    Thanks for the response - I've just tried this and when I put in

    sound = audio_play_sound();

    it says wrong number of arguments to function or script, ie. it doesn't like that I've left it blank - am I missing something / was I supposed to put something in the brackets?
     
  4. Crispywrists

    Crispywrists Guest

    Essentially what I want is:

    if !audio_is_playing(snd_outtheway1, snd_outtheway2, snd_outtheway3) { audio_play_sound(choose(snd_outtheway1, snd_outthway2, snd_outtheway3), 0,0);

    but it wont let me add multiple sound IDs after audio_is_playing, if that makes sense?
     
  5. Crispywrists

    Crispywrists Guest

    Found my answer! It was a simple one but one I shall never forget - putting the && and asking each question before each check, rather than grouping them all together like above:

    if !audio_is_playing(snd_outtheway1) && !audio_is_playing(snd_outtheway2) && !audio_is_playing(snd_outtheway3) { audio_play_sound(choose(snd_outtheway1,snd_outtheway2,snd_outtheway3),1,0) }
     
  6. Bingdom

    Bingdom Googledom

    Joined:
    Jul 1, 2016
    Posts:
    1,678
    Oh, definitely fill the values for audio_play_sound! I was lazy so I left it blank.
    First line should be
    Code:
    audio_play_sound(choose(snd_outtheway1, snd_outtheway2,snd_outtheway3), 0, false);
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice