PDA

View Full Version : why does my array have a blank element?


snilbert
07-29-2003, 06:21 PM
Hi, I'm trying to make an array of the numbers 1 to 100 to correspond with 100 movieclips with the same name. Then I'm trying to randomise this list, and then select however many from the new ordered list.

So far I'm just working in trace mode to test it, but I've noticed that each time I try and create the new list, among the numbers 1 to 100, is a blank space, if you understand what I mean.

I think it's something to do with actionscript counting the first element as 0, whereas in my for loop creating the numbers, I start from 1 and make all numbers less than 101. I've noticed that when I start this from 0 and limit it to numbers less than 100, I don't get the blank in the randomised list.

I attach it below. I want to be able to do it without having to renumber my movieclips from 0 to 99, which will be time consuming. Is it possible, without getting the blanks?

onClipEvent (load) {


list = new Array();
for(i=1;i<101;i++){
trace(list[i] = i);
}

randomList = new Array();
while (list.length>1) {
r = int(Math.random()*list.length);
randomList.push(list[r]);
trace(list.splice(r,1));

}

}



If you scroll down the output window you'll see the blank.

Much appreciate any wisdom anyone can show me...:cool:

dzy2566
07-29-2003, 06:47 PM
onClipEvent (load) {


list = new Array();
for(i=1;i<101;i++){
trace(list[i-1] = i); // <---- I added a -1 to your index ref
}

randomList = new Array();
while (list.length>1) {
r = int(Math.random()*list.length);
randomList.push(list[r]);
trace(list.splice(r,1));

}

}

I think that should do it. This way you begin populating your list at index 0, but it's still with the number 1. When you get to i=100, it will be entered into index 99. At i=101, you drop out of the loop. When referencing your movie clips, you just have to remember each clip is stored in an index 1 less than its name. Is that what you were looking for?

black
07-31-2003, 12:31 AM
i think you forgot to value list[0] :)

freddycodes
07-31-2003, 02:05 PM
Yep like black said you forgot to add the item list[0],

I used this and it worked fine.


onClipEvent (load) {
cnt = 0;

list = new Array();
for(i=1;i<=100;i++){
trace(list.push(i));
}

randomList = new Array();
while (list.length>0) {
r = int(Math.random()*(list.length-1));
randomList.push(list[r]);
cnt++;
}

}