Array - Random Shorting


/ Published in: ActionScript 3
Save to your folder(s)

Array - Random Shorting


Copy this code and paste it in your HTML
  1. //first create your array with songs
  2. var songsArray:Array = new Array();
  3. songsArray.push("Various Artist - Song1.mp3");
  4. //...etc
  5.  
  6. //next create random array
  7. for (var i:int = 0; i < songsArray.length; i++)
  8. {
  9. pickArray[i] = i;
  10. }
  11. //and sort it with your custom sort function
  12. pickArray.sort(randomSort);
  13.  
  14. function randomSort(elementA:Object, elementB:Object):int
  15. {
  16. return Math.random() * 10 - 5;
  17. }
  18.  
  19. //let say playNextSong function is initiated
  20. //by the user clicking on "next" button
  21. var counter:uint = 0;
  22. function playNextSong(e:MouseEvent):void
  23. {
  24. //pick some random song but make sure we won't go out of range
  25. counter = (counter == songsArray.length - 1) ? 0 : counter + 1;
  26. playSong(songsArray[pickArray[counter]]);
  27. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.