4 Replies Latest reply on Oct 18, 2008 7:56 AM by kglad

# swapDepths() strangeness

Hello,

I’ve been trying to get swap depths working for a long time and it seems to be giving me random results.

My problem is that I have a bunch of clips on the stage that are added randomly at random y-positions. The higher y positions must be a higher depth then the ones at a lower y position. Basically, bottom clips, overlap clips above it.

The first way I tried was that every time a new clip is added (they aren’t added all at once, but via user interaction) was to store all the chips into an array and sort them by y-position, so the lowest value y (the highest clip on the stage) would appear first. Then I go through the whole array again and call “clip.swapDepths(parentClip.getNextHighestDepth())”.

My reasoning was that the first clip would get set to the nextHighestDepth overlapping all clips. Then the next clip in the array would get set to the nextHighestDepth, overlapping the previous clip before, and so forth until the last clip in the array would get set to nextHighestDepth, overlapping all of the clips.

The result of that ended up to be very odd looking like:
Index 0 – depth 4
Index 1 – depth 1
Index 2 – depth 0
Index 3 – depth 2
Index 4 – depth 5

Notice that it also skips depth ‘3’.

I then discovered that I can set depths directly by passing in a number And if a clip existed in that depth value, it would just get swapped instead of replaced. So I then tried going (after sorting): clip.swapDepths(counter); -where counter is the array index it is in:

for(var counter:Number = 0; counter < array.length; counter++){
array[counter].swapDepths(counter);
}

But that resulted in about the same result as the first method.

Just for fun, I decided to go (in my above loop) “array[counter].swapDepths(counter * 2);”

But when I traced out the depth, I got depths like 3,5,9,11 –not even numbers.
This leads me to conclude there is something not right with my expectation of swapDepths().
Any ideas on what's going on with swapDepths() or a better way to solve the problem?
• ###### 1. Re: swapDepths() strangeness
store you movieclips in an array (which you've already done), use a custom sort function to sort that array by the movieclip's _y property and store the depths of your movieclips in an array an sort in ascending (or descending) order. finally, iterate through your sorted movieclip array swapping each movieclip's depth with the corresponding depth in your array of sorted depths.
• ###### 2. Re: swapDepths() strangeness
Hm, not sure how that can work, but I tried it anyways. It doesn't seem to work, here is what happens (I trace out the array that contains the clips to get this list)

index - clip name on the stage --------------------- y value -- it's depth
*0----(_level0.chipParentClip.chipStack0-4)---------- 456.75 --------0

*0----(_level0.chipParentClip.chipStack0-3.5)-------- 443.4 ---------0
1-----(_level0.chipParentClip.chipStack0-4)---------- 456.75 --------1

Third clip (so far so good)
*0----(_level0.chipParentClip.chipStack0-3)---------- 430.1 ---------0
1-----(_level0.chipParentClip.chipStack0-3.5)-------- 443.4 ---------1
2-----(_level0.chipParentClip.chipStack0-4)---------- 456.75 --------2

Adding a forth clip (not good)
*0----(_level0.chipParentClip.chipStack0-2.5)-------- 416.8 ---------2
1-----(_level0.chipParentClip.chipStack0-3)---------- 430.1 ---------1
2-----(_level0.chipParentClip.chipStack0-3.5)-------- 443.4 ---------0
3-----(_level0.chipParentClip.chipStack0-4)---------- 456.75 --------3

Fifth clip
0-----(_level0.chipParentClip.chipStack0-2.5)-------- 416.8 ---------2
1-----(_level0.chipParentClip.chipStack0-3)---------- 430.1 ---------1
2-----(_level0.chipParentClip.chipStack0-3.5)-------- 443.4 ---------0
3-----(_level0.chipParentClip.chipStack0-4)---------- 456.75 --------3
*4----(_level0.chipParentClip.chipStack0-4.5)-------- 470.05 --------4

Sixth Clip
0-----(_level0.chipParentClip.chipStack0-2.5)-------- 416.8 ---------5
1-----(_level0.chipParentClip.chipStack0-3)---------- 430.1 ---------1
*2----(_level0.chipParentClip.chipStack1-4)---------- 431.5 ---------2
3-----(_level0.chipParentClip.chipStack0-3.5)-------- 443.4 ---------0
4-----(_level0.chipParentClip.chipStack0-4)---------- 456.75 --------4
5-----(_level0.chipParentClip.chipStack0-4.5)-------- 470.05 --------3

(The starred clip is the new clip that is added)
My code snippet on how I did it is below

• ###### 3. swapDepths() strangeness
This is a new post as I seem to have discovered something new.

I was making a test with a new fla and everything (to isolate it and easier to test/fix) and I tried my first method again (as it was short and easy to try).

And it worked. o.O

Back in my original code, upon further experimenting I discovered if I took those periods out of the movieclip’s name, it works fine.

So it was something with the clip’s name. Anyone can confirm/know why periods are bad? My random guess is inside the swapDepths() method, it cases some clip reference to go funny.

The code I used in the end is below.
• ###### 4. Re: swapDepths() strangeness
dots are reserved (by flash) symbols used to indicate demarkers between objects, properties and methods.