# Dinamically assigned depths

**URL:** <https://forum.kirupa.com/t/dinamically-assigned-depths/102370>\
**Category:** Uncategorized\
**Created:** [February 23, 2004, 4:51am UTC](https://forum.kirupa.com/t/dinamically-assigned-depths/102370 "2004-02-23T04:51:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![El\_Bicho](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@El\_Bicho](https://forum.kirupa.com/u/El_Bicho)\
**Post date:** [February 23, 2004, 4:51am UTC](https://forum.kirupa.com/t/dinamically-assigned-depths/102370/1 "2004-02-23T04:51:19Z")

</div>

This code works fine:

```auto

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:

```auto

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2004, 7:46am UTC](https://forum.kirupa.com/t/dinamically-assigned-depths/102370/2 "2004-02-23T07:46:41Z")

</div>

Whats with this piece of code?

```auto
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:

```auto

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

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2004, 3:27pm UTC](https://forum.kirupa.com/t/dinamically-assigned-depths/102370/3 "2004-02-23T15:27:14Z")

</div>

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

```auto

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.

```auto

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:

```auto

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.
