mheavers
mheavers

Reputation: 30158

flash as3 - how to get next element in an array

I have an array of five possible videos that can play called videos, and an array of active videos called activeVideos. When one video finishes playing, I need to take its index, find its location in the activeVideos array, and get the next value in the array. If there are no more values in the array, I need to grab the first value. So essentially:

activeVideos = [0,1,3]
videos = [video1, video2, video3];

function nextVideo(e:Event){
    var curIndex = videos.indexOf(currentVideo);
    var next = activeVideos.find(curIndex).nextValue // pseudo-code - need to locate the position of the current video within the active array, select the next element, and call show video.
    showVideo(videos[next]);
}

How do I do this?

Upvotes: 0

Views: 1624

Answers (3)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

function nextVideo(e:Event){
    var curIndex = videos.indexOf(currentVideo) + 1;
    var nextVideo = curIndex >= videos.length ? videos[0] : videos[curIndex];
    showVideo(nextVideo);
}

Upvotes: 0

daformat
daformat

Reputation: 786

Maybe you can try this at home, I must confess my flash might be a bit rusty since the advent of the html5, css3 + js hype :)

nextIndex = -1;
nextIndex = ( (curIndex+1 == videos.length) ? 0 : curIndex+1 );
if (nextIndex > -1){
  var next = activeVideos[nextIndex];
  // ... do your stuff 
}

Upvotes: 0

j_mcnally
j_mcnally

Reputation: 6968

activeVideos = [0,1,3]
videos = [video1, video2, null, video3];

function nextVideo(e:Event){
    var curIndex = videos.indexOf(currentVideo);
    var next = activeVideos[curIndex+1] || activeVideos[0];
    showVideo(videos[next]);
}

Upvotes: 1

Related Questions