Set your tracks up as an array of objects:
var track1:Object = {
track: 'Don\'t stop believing',
artist: 'Journey',
file: 'dont_stop_believing.mp3'
};
var track2 //same as above
var tracks:Array = [track1, track2, ...];
You could really create a Track
class, but it sounds like you aren't to that point yet.
Instead of making your playTrack
function actually be the mouse event handler, you should separate it out so that it can be used universally no matter how the track begins to play (i.e. clicking on that track's button, clicking on the next button, or after the previous song ends). Write a separate function just to handle the mouse event (i.e. clickTrack()
), which will call your playTrack()
function.
Setting your tracks up within the array will allow you to keep note of the indices of each track (including the currentTrack
) as a number. That way you can iterate through the tracks by just incrementing the currentTrack
variable.
This way, you can set your playTrack()
function up to take a trackNumber
parameter (i.e. playTrack(1)
. Then just use that parameter to reference the index of the track you want to play within the tracks
array. Remember that arrays are on a 0 index meaning that the first element is index [0], the second is [1], etc. So you'll either have to write your playTrack()
function in that way, or convert by subtracting 1.