How do I set depth higher for movieclip

I know how to swapDepth between two movieclips

myMC1_mc.onRelease = function() {
    this.swapDepths(myMC2_mc);
};
myMC2_mc.onRelease = function() {
    this.swapDepths(myMC1_mc);
};

but what if there are 20 clips on stage.
Is there some easier way than this:

myMC1_mc.onRelease = function() {
    this.swapDepths(myMC2_mc);
 this.swapDepths(myMC3_mc);
};
myMC2_mc.onRelease = function() {
    this.swapDepths(myMC1_mc);
 this.swapDepths(myMC3_mc);
};
myMC3_mc.onRelease = function() {
    this.swapDepths(myMC1_mc);
    this.swapDepths(myMC2_mc);
};

petersegoAsked:
Who is Participating?
 
TanLiHaoConnect With a Mentor Commented:
It's possible, but it's weird how you want to swap depths with all the other movieclips.

Do you want actually want to swap to the highest depth or what? You see, it's actually weird.

Let me give an example,

MC1 Depth: 0
MC2 Depth: 1
MC3 Depth: 2

So if you clicked MC1, it will first swap depth with MC2, then it's depth become 1, then it swap with MC3 it's depth becomes 2. This effectively means that MC2's depth is pushed down by 1 and MC1 have the highest depth. is this actually what you want?

Anyway, here's the shortened code for swapping depths with all other MCs.

for (var i:Number = 1; i <= 20; i++) {
    this["myMC" + i + "_mc"].onRelease = function():Void {
        for (var j:Number = 1; j <= 20; j++) {
            var MC:MovieClip = this["myMC" + j + "_mc"];
            if (MC != this) {
                this.swapDepths(MC);
            }
        }
    }
}
0
 
TanLiHaoCommented:
On second thoughts, maybe it isn't that weird after all.
0
 
petersegoAuthor Commented:
Well maybe it IS weird to use the swapdepth method.
What I simply want to achieve is to make the movieclips move to the topof the rest, when they arer olled over.
Maybe swapDepth isnt the best method.... though it looks as if your solution is perfect for me....
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
TanLiHaoCommented:
Hmm ah yes, something made me think that it is weird but I couldn't figure it out. That's right, it seems pretty weird to use swapDepths to place movieclips at the top.

There is another way to do this, it is to check all the depths of the movieclips and see which movieclip have the highest depth, then set the depth to above that depth. However, there is also a max depth. Furthermore, this is a very messy way of doing it. Or the other way is simply set the depth of the movieclip to the movieclip with the highest depth and shift each movieclip's depth down by 1(makes sense but doesn't really mean that all movieclips all separated by 1).

This depth management problem exists in AS2 and is confusing, so I guess the solution I provided should be ok and is probably the best I can think of now. Yes, the other methods I mention may make more sense but certainly don't work well in all cases. swapDepths work well in all cases so it is better.

You might consider using AS3, because there are better functions to manage depth and have built-in function(i.e. setChildIndex) to actually move the movieclip to the top-most.
0
 
petersegoAuthor Commented:
Ive just started learning as3, but for now, Im happy with an as2 solution, thanks.
0
 
petersegoAuthor Commented:
If you are still there.
Apparantly there is something wrong. I expected the code to work, but it doesnt.
I simply cant figure out why....
The movieclips (myMC1_mc to myMC4_mc) stay at the same depth when I press them
0
 
TanLiHaoCommented:
Have you tested the code and it worked with other code or the code just don't work at all?

If the code isn't working at all, it may be either a problem with you putting the code at the wrong place(that is, it should be in MainTimeline or inside another movieclip) or you can say that you didn't tell me that the movieclips are not at the root. Otherwise, there may be other problems associated but let's debug this slowly.
I change the code slightly to output some messages, please tell me what messages they give you. Please pardon my indenting but it's really hard to indent in experts exchange since I didn't use the indent feature at the start.

for (var i:Number = 1; i <= 20; i++) {
trace("MovieClip" + this["myMC" + i + "_mc"]);
    this["myMC" + i + "_mc"].onRelease = function():Void {
        for (var j:Number = 1; j <= 20; j++) {
           var MC:MovieClip = this["myMC" + j + "_mc"];
trace("MC" +MC);
            if (MC != this) {
                this.swapDepths(MC);
            }
        }
    }
}

If you get any undefined in the code, then the path to the movieclip is referenced wrongly. Otherwise, like I said, please show me the output messages.

Providing your source file will be easier but if you do not want to, we can try this first.
0
 
petersegoAuthor Commented:
Sure you can see the file.
This is what I get when I trace.

MovieClip_level0.myMC1_mc
MovieClip_level0.myMC2_mc
MovieClip_level0.myMC3_mc
MovieClip_level0.myMC4_mc
MC undefined
MC undefined
MC undefined
MC undefined

The first four lines are at initiation, the next four is when I press one of the lower squares.

It seems that var MC doesnt get a value....
swap.fla
0
 
TanLiHaoCommented:
I apologise, it is my mistake.

Here's the correct working code.

for (var i:Number = 1; i <= 20; i++) {
    this["myMC" + i + "_mc"].onRelease = function():Void {
        for (var j:Number = 1; j <= 20; j++) {
            var MC:MovieClip = _root["myMC" + j + "_mc"];
            if (MC != this) {
                this.swapDepths(MC);
            }
        }
    }
}

However, this code just simply swap the depths with the other three movie-clips, after seeing your .fla, I think you want to make the movieclip on the most top when you click the movieclip. Therefore, you need to change a line of code to fix that.

Here's the code to make the movieclip most on top.

for (var i:Number = 1; i <= 4; i++) {
   this["myMC" + i + "_mc"].onRelease = function():Void {
       for (var j:Number = 1; j <= 4; j++) {
           var MC = _root["myMC" + j + "_mc"];
           if (MC != this) {              
               if (this.getDepth() < MC.getDepth()) {
                   this.swapDepths(MC);
               }
           }          
       }      
   }
}

Here's another snippet of code I will prefer you to use if you just want to place the movieclip to the most top by just swapping the movieclip at the most top and not swap depths with other movieclips along the way.

var highestDepth:Number = -Infinity;

for (var i:Number = 1; i <= 4; i++) {
   this["myMC" + i + "_mc"].onRelease = function():Void {
       for (var j:Number = 1; j <= 4; j++) {
           var MC = _root["myMC" + j + "_mc"];
           if (MC != this) {              
               if (MC.getDepth() > highestDepth) {
                   highestDepth = MC.getDepth();
               }
           }  
       }
       if (this.getDepth() < highestDepth) {
           this.swapDepths(highestDepth);
       }
   }
}

You can choose to use any of them, both works, I didn't benchmark them to see the performance but if you want, you can try it out yourself.
0
 
petersegoAuthor Commented:
Thanks, both works perfect...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.