SwapDepth of MC's with buttons (flash5)

Yeah, I know, there is a tutorial about changing stacking order (http://www.kirupa.com/developer/actionscript/swapdepth.asp), but the makers were so kind to use Mx files for the .fla’s… so it turned out not to be that helpful :sigh:

Flash 5

Anyway…

I got myself lost in a merry SwapDepth mixup.

I have been trying to tweek this tutorial to my own needs:

http://www.actionscript.org/tutoria...hs/index2.shtml

what I want is simple.

I have 4 images.
I have 4 buttons.

When I click one of the 4 buttons, the image related to that button has to move to the hightest position. And ofcourse the previous highest one should be the one under the new highest image.

Well, I tried to fix the script, but for some reason it gets all confused and starts switching images, and sometimes none.

Can someone take a peek at the .fla enclosed and tell me what I am missing here???

Thanks so much !

just use

swapDepths(++depth);

each time you swapDepths with this number, it is incremented by one. The last window you swapped to the top would be, les say 10, then pressing another would bring the new one up to 11. The one at 10 remains the next highest. Pressing another would bring it to 12, making 11 the next highest and 10 the next after that.

Any variable can be used; I used “depth” because, well, it makes sense

hold on, hold on, what?

sorry, please keep in mind my fairly basic knowledge of AS, so slow down a notch :slight_smile:

Okay, so I should keep the remaining code and instead of the current code I placed on the buttons:

_root.window3.swapDepths("_level0:window"+_level0:highest);
_level0:highest = substring(_name, 7, 1);

i should use:

swapDepths(++depth);

Or am I (as happens quite frequently with AS) completely and utterly wrong?

and what should I do with the action I placed on frame 1 of the root?

Thanks for your help!

forget all that highest code…

swapDepths can do two things. It can switch the depths of two movieclips so that their arrangement is switched OR it can swap the depth of a movieclip to a numerical value. This numerical value is what my example is doing. The number itself is stored in a variable called depth. This variable depth is incremented by one every time

swapDepths(++depth)

is called as a result of the ++ increment operator. Depending on where the swapDepths is being called, you may want to use something like ++_root.depth instead so that you know each time a depth is swaped, its using the same variable to swap with and therefore going to the next number up.

Movieclips you place on the stage in flash for example, start with a depth of something like -16,000. Chances are you wont be placing 16000 clips (or 8000 - probably more than 100 for that matter) on the screen so starting at a depth of 1 is just fine. If you dont declare _root.depth to be a value anywhere before you use it, when swapDepths(++_root.depth) is called, it will start at 1

As I described before, if you use this method on all your windows or images or whatever it is you are swapping, then each one will be consecutively higher then the next in the order they were swapped.

Dont worry about highest and lowest and all that mess, just use movieclipInstance.swapDepths(++_root.depth) when you swap.

thanks a lot :slight_smile:

I will try if I can get it to work, if not, well, then i’ll be back :wink:

if I do, also by the way.

it works briliantly!!!
thanks a lot :slight_smile:

last question (oh no, he thinks :wink: )

If I want a MC which I want to stay on top of those images (for example a watermark), I would have to set its level to very high, but how do I do that?

or slightly more sophisticated, would it be possible that this MC which stays at the top level would each time be one level higher than the highest image? So this MC would move along with the images changing levels.

In any case already thanks a lot for your wonderful help!!!

the one thing about using swapDepths with a number value is that it does put it pretty much over anything within that scope including all other movieclips you placed there no matter what layer they are in. There are basically 2 ways to have things on top

  1. use swapDepths on whatever it is you want to be on top to be a higher depth than the other objects you are using swapDepths with by either a) swaping initially to something high like 10000 or b) consistently swaping to be the highest depth +1 whenever something else is swapped.

  2. keeping all of your swapped clips in their own movieclip. Since they are in this ‘container’ movieclip, their position to other objects are in respect to that of the container. Since the container itself is never swapped, it stays where it is in respect to other movieclips around it. Swapdepths only effects depths in any one scope/timeline

ah okay, so if I would keep the images/mc in an external SWF that I load into a MC on the main.swf, there shouldn’t be a problem, right?

thanks :slight_smile:

ps. do you accept American Express… :stuck_out_tongue:

well you dont have to make it external, just put them in a movieclip

uhm, I actually want to :slight_smile:

(loading matters etc)

it must be the amount of wine I drunk, cos I cannot believe I missed this… but, it doesn’t work the way I hoped it would be, actually…

the problem is, it does swapdepth between two MC… and there you have the problem…

Since I want the previous top image visible under the new topimage (due to a fadein effect), you can imagine what happens if for example I switch the number one with the number 4.

Image number 1 moves to the number 4 spot and the new image fade in over the number 2 image.

How can I avoid this, or maybe simply move a MC to the toplevel???

You lost me… using number values is not swapping between two different clips, just using numbers. I dont know what you’re doing now :slight_smile:

maybe I understood what caused the mess.

with swapDepths(++depth)

you use the ++depth as some kind of counter, right?

I used your code as follows:

on (press) {
_root.window0.gotoAndPlay(1);
_root.window0.swapDepths(++root.depth);
_root.window0.gotoAndPlay(2);
}

>>> see down for explaination :slight_smile:

But this gives the problem.

So I tried with setting a ‘var’



on (press) {
    topDepth++;
    _root.window0.gotoAndPlay(1);
    _root.window0.swapDepths(topDepth); 
    _root.window0.gotoAndPlay(2);
}

legenda:

gotoandplay 1 moves the mc to an image with alpha 0%
gotoandplay 2 moves the mc to the frame where a tween starts which transforms the mc from alpha 0 to 99%

However, when I click the different buttons a couple of time, all of a sudden the fadein transition in between them goes really slow and the flash anim freezes with pauzes. I can’t click the buttons any more.

is it my code, or what could be the reason for it?

the topdepth thing is the same thing I initially suggested, just a different variable

The problem you are having is that when you press your buttons a few times, you’r not stopping your fading effects so you are getting fading compounded on fading compounded on fading which is making flash a little unhappy. What you would have to do is stop all of your fading clips putting them at 100% when you bring a new one to the top. Really all you need to fade is that top clip keeping the one beneath 100%. That will be a fade directly from the lower clip to the full topmost clip.

your first gotoAndPlay is actually redundant since all code in a button happens within the same frame as when the button is pressed. Having a clip gotoAndPlay 1 and then immediately thereafter telling it to gotoAndPlay 2 makes the gotoAndPlay 1 meaningless.

what you’ll probably want to do is either set up a loop to tell all the movieclips to stop at a frame of 100% alpha when a new button is pushed or save the most recently raised clip in a variable and use that variable telling it (the movieclip it references) to goto and stop at the 100% alpha frame preventing the multifading slowdown you seem to be encountering.

Something like:

on (press) {
lastWindow.gotoAndStop(lastWindow._totalframes)
_root.window0.swapDepths(++_root.depth);
_root.window0.gotoAndPlay(2);
lastWindow = _root.window0
}

assuming the last frame (_totalframes) in eash ‘window’ is the fully visible 100% alpha frame. This tells lastWindow to stop at its last frame so its no longer fading, brings window0 to the top, tells it to play its fade over the lastWindow then sets lastWindow to be window0. Then if window1 is pressed next, window0 (which would be the lastWindow) would go to its fully visible frame, window1 would be knocked up above it and then played from 0-100 above that, where lastWindow is then re-set to be window1 so the same thing can happen again the next time a button is pressed.

thanks for your help :slight_smile:

well, I put the gotoandplay(1) for the following reason.
you can’t see it, but on frame 1 and the last frame of the MC (so the one that is being swapped depth) I put a stop action.

so on Release it is first being set to alpha 0 (stop)
then it swap depth and after that it goes to 2 to play the fade in tween (stop)

that is also the reason of the gotoandplay(1). I assumed that after being clicked once, the MC would remain in the last frame of that tween and MC (being alpha 99% - to avoid the famous jpg tween shifting problem). So in order to let it go back to the alpha 0 state, I send them before swapping the MC back to frame 1.

I thought that the stop() action would be enough to cancel the tween creating problems for flash, but I was wrong?

By the way, I wait till the fade in of the MC has completed, before I click the new button, but the problem persists. I even thought that maybe the order of actions could cause the problem or so.

But I will try your suggestion and let you know if the problem is fixed with it :slight_smile:

my current efforts, without your latest suggestion, just to be more clear.

ps. see the previous thread page for my reply to your post :slight_smile:

back again.

I used your code for the 4 buttons, but now it seems the problem happens even quicker than before. I can click the four buttons, but after that it freezes temporarily and the fade ins play shockingly… :frowning:

i will also attach the new files, though nothing else but the button code changed.

it works fine for me. No freeze-ups or anything. The only thing I see that should be done now is preventing the same button being pressed more than once so you dont get that re-fade. For that you just use an if on all your buttons.

if (lastWindow != window0) … for window0

if (lastWindow != window1) … for window1 etc

Itd also be easier to do all this through a function on the main timeline since this code is virtually identical for all the buttons:


FadeIn = function(windowNum){
	var currWindow = this["window" + windowNum];
	if (lastWindow != currWindow){
		lastWindow.gotoAndStop(lastWindow._totalframes);
		currWindow.swapDepths(++depth); 
		currWindow.gotoAndPlay(2);
		lastWindow = currWindow;
	}
}

then on your buttons just use

on (press) {
FadeIn(0)
}

on (press) {
FadeIn(1)
}

etc

strange that you don’t have the same problem.

thanks for the function :slight_smile:

I guess I put this on the timeline of wherever I put the MC, right?
Since in the final version I will put the mc’s in an external SWF that I will load in an MC on the main.

And from another SWF on that main, I will trigger those mc’s.

But still I put the code on the main of where there are the MC’s right? (apart from having to see that the right path are used).

you’d put that code in the timeline which the buttons exist in, since it is those buttons using the code. If your buttons are in _root, place that code in _root.