• 0

ActionScript- array.length - 1?


Question

var AmountPic:Number = 2;
var picList = new Array();
var currentElement:Number = 0;


for(i=0; i < AmountPic; i++ ) {
	var destination:String = "gal" + (i+1) + ".swf";
	picList.push(destination);
}

stuff_mc.loadMovie(picList[currentElement]);

neste.onRelease = function() {
	if (currentElement < picList.length-1) {
		currentElement++;
	}
	else {
		currentElement = 0;
	}
	stuff_mc.loadMovie(picList[currentElement]);
}

When I come to the last element in the index I want it to go to the first index again, thats what the code in "neste.onRelease = function()" do. But i don't understand the first part with

if (currentElement < picList.length-1) {
		currentElement++;
	}

picList.length-1.. why "-1" ?

thx

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Arrays start at 0 in most programming languages. If you have an array of 3 elements, you have 3 positions available - positions 0, 1 and 2. As a result, fetching the length would return 3, so you need to subtract 1 to get the last element, the element at position 2.

Link to comment
Share on other sites

  • 0
I think all languages zero index... except for databases.

The original VB allowed you to create arrays that started at 1 too. Don't know about VB.NET, but I would assume not.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.