[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I set depth higher for movieclip

Posted on 2009-12-20
10
Medium Priority
?
1,267 Views
Last Modified: 2012-06-27
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);
};

0
Comment
Question by:petersego
  • 5
  • 5
10 Comments
 
LVL 14

Accepted Solution

by:
TanLiHao earned 2000 total points
ID: 26094158
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
 
LVL 14

Expert Comment

by:TanLiHao
ID: 26094161
On second thoughts, maybe it isn't that weird after all.
0
 

Author Comment

by:petersego
ID: 26094666
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Expert Comment

by:TanLiHao
ID: 26094756
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
 

Author Closing Comment

by:petersego
ID: 31668247
Ive just started learning as3, but for now, Im happy with an as2 solution, thanks.
0
 

Author Comment

by:petersego
ID: 26095112
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
 
LVL 14

Expert Comment

by:TanLiHao
ID: 26095419
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
 

Author Comment

by:petersego
ID: 26104838
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
 
LVL 14

Expert Comment

by:TanLiHao
ID: 26110492
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
 

Author Comment

by:petersego
ID: 26110770
Thanks, both works perfect...
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question