Guest User

how to play audio in random succession in AS3

a guest
Sep 7th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 2.78 KB | Source Code | 0 0
  1. // set variables for sound channels, a counter and a random number for an audio loop to play
  2. var channelA:SoundChannel;
  3. var musicA:a = new a();
  4. var channelB:SoundChannel;
  5. var musicB:b = new b();
  6. var count = 0;
  7. var randomsong:int;
  8.  
  9. // plays the first loop called "a" in the library (I named it like this for simplicity) one time when you open the flash
  10. channelA = musicA.play(0, 1);
  11.  
  12. // this is an event listener that decides what function to trigger for the file in the library by what method
  13. // in this case when the channelA (the first audio loop) finishes playing it triggers a function called "onComplete" which is defined below
  14. channelA.addEventListener(Event.SOUND_COMPLETE,onComplete);
  15.  
  16. // this motherfucker is where the magic happens
  17. function onComplete(e:Event):void
  18. {
  19.    
  20.     // trace shows in the output window what number it is on for information purposes, it starts with "count = 0"
  21.     trace ("count = " + count);
  22.    
  23.     // this generates a random number from 1 to 4, if you don't add " + 1" it generates 0 to 3
  24.     randomsong = Math.random() * 4 + 1;
  25.    
  26.     // same thing as the previous trace, but this one shows the ranodmly generated number
  27.     trace("random = " + randomsong);
  28.    
  29.     // when the counter equals 0 it starts below sequence
  30.     if (count == 0)
  31.     {
  32.        
  33.         // adds + 1 to the counter. It is the same as if you write "count = count + 1"
  34.         count++
  35.        
  36.         // plays the first audio loop again once
  37.         channelA = musicA.play(0, 1);
  38.        
  39.         // adds the listener again so the whole function can run all over again from the beginning but with a higher counter.
  40.         // You put it here again because the first listener isn't gonna trigger anymore because the audio loop only played once. If you play the loop more times then you'll end up with triggering it over and over with overlapping audio loops
  41.         channelA.addEventListener(Event.SOUND_COMPLETE,onComplete);
  42.     }
  43.  
  44.     // when the randomly generated number is HIGHER OR EQUAL THAN ">=" 3 OR "||" the counter is EQUAL "==" to 3. FYI the operator for AND is "&&"
  45.     else if (randomsong >= 3 || count == 3)
  46.     {
  47.        
  48.         // resets the counter back to 0
  49.         count = 0;
  50.        
  51.         // plays the second audio loop (with whistling) once
  52.         channelB = musicB.play(0, 1);
  53.        
  54.         // adds the listener to the second audio loop. This time with counter reset to 0
  55.         channelB.addEventListener(Event.SOUND_COMPLETE,onComplete);
  56.     }
  57.  
  58.      // otherwise do this sequence. So if the randomly generated number is LESS THAN 3 and the counter is also NOT 3. FYI the opretaor for NOT is "!="
  59.     else
  60.     {
  61.        
  62.         // adds + 1 to the counter
  63.         count++
  64.        
  65.         // generates a new number again
  66.         randomsong = Math.random() * 3 + 1;
  67.        
  68.         // plays the first audio loop once
  69.         channelA = musicA.play(0, 1);
  70.        
  71.         // adds the listener
  72.         channelA.addEventListener(Event.SOUND_COMPLETE,onComplete);
  73.     }
  74. }
  75.    
  76.  
Tags: flash
Add Comment
Please, Sign In to add comment