Dinamically assigned depths

This code works fine:


depths = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for (var i = 0; i<depths.length; i++) {
	_root.attachMovie("clip", "clip"+i, depths*);
	_root["clip"+i]._x = 30*i;
}

But when I try to change depths in a random way it doesen´t:


depths = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
depths2 = [];
while (depths.length) {
	depths2.push(depths.splice(Math.floor(Math.random()*depths.length), 1));
}
depths = depths2;
for (var i = 0; i<depths.length; i++) {
	_root.attachMovie("clip", "clip"+i, depths*);
	_root["clip"+i]._x = 30*i;
}

Instead of appearing the 10 clip appears only one.

Any hint?

Thanks.

Whats with this piece of code?

depths = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
depths2 = [];
while (depths.length) {
        depths2.push(depths.splice(Math.floor(Math.random()*depths.length), 1));
}
**depths = depths2;**//whats up with this line?

Are you trying to randomize the depths array and then attach the movies? If so, try something like this:


Array.prototype.randomize = function() {
        return this.sort(function (a, b) {
                return (Math.floor(Math.random()*2) == 0) ? 1 : -1;
        });
};
sorted_array = depths.randomize();

Thanks, your example just does the job but I´m still confused why my example doesen´t work.


depths = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
depths2 = [];
while (depths.length) {
	depths2.push(depths.splice(Math.floor(Math.random()*depths.length), 1));
}
depths = depths2;
trace(depths);//returns 6,5,8,3,9,4,7,1,0,2

Here the array is randomized but it doesen´t work in the attachMovie line…

If I add this line after the trace statement (it has no use doing this, just for confirmation) it works.


depths = [6, 5, 8, 3, 9, 4, 7, 1, 0, 2];

Here I have a similar example. Let´s say I want to do the same but instead of using the 10 clips I want to use 3:


depths = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
Array.prototype.rand = function() {
	temp = [];
	elements = this.length;
	while (this.length>elements-3) {
		temp.push(this.splice(Math.floor(Math.random()*this.length), 1));
	}
};
depths.rand();
trace(temp)//returns 7,0,8
for (var i = 0; i<temp.length; i++) {
	_root.attachMovie("clip", "clip"+i, temp*);
	_root["clip"+i]._x = 30*i;
}

The same happens; it randomizes the array but it doesen´t work with the attachMovie line.