00:00
-:-
--:-- / --:--
Newgrounds Background Image Theme

M4deadpool just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS: Swap Depths

11,340 Views | 27 Replies
New Topic Respond to this Topic

AS: Swap Depths Aug 19, 2005


AS: Main

Swap Depths

This is going to be relativly short because the tutorial is not very complex, but it will be helpful. Check out the examples at the end of the page to see what you will learn.

What does swapDepths do?

swapDepths will dynamically swap the depth of a movie clip, so you can bring MCs forwards or backwards in order. This can be useful for things where you may need to be in front of something at some point, and behind it at another. (NOTE: If you have played Alien Hominid on the console, one of the earlier levels has a few hedges where you are behind and then in front of, this is the same principle.)

How can I use swapDepths?

The following codes are depth related, and may help you if you are using swapDepths.

INSTANCE.swapDepths(target) - Swap INSTANCE with the instance of the MC that you will be swapping depths (eg this), and swap target with either an instance name or a number, the MC will swap depths with target.

INSTANCE.getDepth() - This will get the depth of the INSTANCE for use in your code.

_root.getNextHighestDepth() - This will get the next highest depth, for use in your code.

An example of swapDepths:

onClipEvent (enterFrame) {
thisDepth=this.getDepth(), otherDepth=_root.INSTANCE.getDepth();
if (_y>_root.INSTANCE._y && otherDepth>thisDepth) {
this.swapDepths(_root.INSTANCE);
}
if (_root.INSTANCE._y>_y && thisDepth>otherDepth) {
this.swapDepths(_root.INSTANCE);
}
}

That code is a basic swap depths code, where the MC the code is placed upon will be either in front or behind the INSTANCE movie clip.

Examples

Drag and drop the snowmen.
Move with the arrow keys around the block/wall.

Useful links
Google
Macromedia
Kirupa


Sup, bitches :)

BBS Signature

Response to AS: Swap Depths Aug 19, 2005


dude it's one command... this is like making a AS: _x or AS: _width

you didn't cover the most importent fact that flash does all it's checks based on the object's depth

objects with higher depth will be shown last, have their AS executed last, and updated last.

Response to AS: Swap Depths Aug 19, 2005


At 8/19/05 11:47 AM, Inglor wrote: dude it's one command... this is like making a AS: _x or AS: _width

I know, but I saw the 'AS or not?' topic, I realized not many people know about depths in flash, as long as it helps people then it's good.

If you want to add anything, then go ahead.


Sup, bitches :)

BBS Signature

Response to AS: Swap Depths Aug 21, 2005


Should probably add to this that.

Manually placed objects (placed during authoring) are given depths ranging from
about -16,000 to -1

Objects with depths in that range are Not dynamically removable, in order to remove them using removeMovieClip or deleteMovieClip you have to first swap their depths to within the 0 to about 1 000 000 range.

Dynamically placed (attachMovie , createEmptyMovieClip , duplicateMC etc.) objects are given depths from 0 to a little above 1 million, they can be dynamically removed.

Objects with depths higher than the 1 million (rough) mark are classed as overflow, and dynamic removel is diabled again. The overflow depth range is rarely used.

Response to AS: Swap Depths Aug 21, 2005


At 8/21/05 08:15 AM, T-H wrote: Should probably add to this that.

Ah yeah, that is pretty good for people to know.


Sup, bitches :)

BBS Signature

Response to AS: Swap Depths Aug 23, 2005


can a depth have a decimal?

Response to AS: Swap Depths Aug 23, 2005


---no

Response to AS: Swap Depths Aug 23, 2005


I'm no depth god but i'm gonna say no.


These new signatures can suck on mah balls. My lolis don't fit in. Lol wut what are you guys still doing on NG, move on.

BBS Signature

Response to AS: Swap Depths Aug 23, 2005


Depths are always whole numbers


BBS Signature

Response to AS: Swap Depths Oct 21, 2005


Im going to read into this -liam- for my game. Rememberz? Lol.

When I asked how to remove an MC, you told me to look into this. But I dont see how they 2 connect?


"Actually, the server timed out trying to remove all your posts..."

-TomFulp

Response to AS: Swap Depths Dec 11, 2005


I got this sign going off into the distance, and at one point it goes behind the ground, i tried this code, and at the point where i swap the depths, the guide motion tween stops. (but it does go behind, just no more tweening.)
Any help = Thxns!

Response to AS: Swap Depths Dec 11, 2005


At 12/11/05 04:15 AM, FlyingPika wrote: I got this sign going off into the distance, and at one point it goes behind the ground, i tried this code, and at the point where i swap the depths, the guide motion tween stops. (but it does go behind, just no more tweening.)
Any help = Thxns!

dont use this for movies. use this for games.
if you have creative control over a movie, swap the depth manually
(move the MC to a lower layer or something)


BBS Signature

Response to AS: Swap Depths Dec 11, 2005


At 12/11/05 04:15 AM, FlyingPika wrote: I got this sign going off into the distance, and at one point it goes behind the ground, i tried this code, and at the point where i swap the depths, the guide motion tween stops. (but it does go behind, just no more tweening.)
Any help = Thxns!

I forgot to mention even if i remove the guide motion, so its just a tween, it still stops moving at the point of swapping the depth, but does swap the depth.
I dont even know where to being, this is much harder to debug than conventional programming X_x

Response to AS: Swap Depths Dec 11, 2005


FlyingPika it sounds like you using this for a movie, this is for games. Just put the sign in a layer above the ground to make it seem like its above the ground, or a layer below the ground, to make it look like its beneath the ground. -.^


"Actually, the server timed out trying to remove all your posts..."

-TomFulp

Response to AS: Swap Depths Dec 11, 2005


At 12/11/05 05:01 AM, IWantSomeCookies wrote: FlyingPika it sounds like you using this for a movie, this is for games. Just put the sign in a layer above the ground to make it seem like its above the ground, or a layer below the ground, to make it look like its beneath the ground. -.^

Yes u write, and thats eaxtly what i didnt want to do X_x having millions of layers gets abit overwhleming, i like to try keep things neat and tidy and avoid redudancy.
Boo flash!

Response to AS: Swap Depths Mar 31, 2006


Hey all... sorry for the bump, but I have a question (probably very stupid) on swapdepth...

Is there any way to use this for a Button, and not an MC? This is for a game of mine under construction, but using:

onClipEvent (load) {
this.swapDepths(99999);
}

This code leads to an error, that the Clip events can only be used for MC instances... is there any way to use a swap depth for a Button? If so, what handler must I use?

I'm probably making a begginer mistake, but any help will be apreciated. Thanks in advance...

James.

Response to AS: Swap Depths Mar 31, 2006


Did you mention that the target for this.swapDepths(bleh) can be an integer and not necessarily another MC instance?

Well, I'm sure someone's picked it up by now.


BBS Signature

Response to AS: Swap Depths Mar 31, 2006


Yes, that was mentioned that one can use an integer (as in my example of 99999... however neither method seems to work (when the thing that I want to change depth of being a button, not an MC...

Response to AS: Swap Depths Mar 31, 2006


An important thing you forgot to mention that will cause problems for alot of people new to depths:

When using swapdepths to switch the depth with another object, the depths are switched, no harm done.

When using swapdepths to assign a depth number to an object, the object becomes dynamic (cant think of a better word for it), meaning it can now be removed by the removeMovieClip() command.

This also means that it is now on top of everything in your movie, and no longer binded to the frame it was first on. That means if you have a movieclip in frame 1 only, swapping its depth to a number will make the movie appear onscreen for the rest of your flash video, even across different scenes. The only way to remove a movieclip with a dynamic depth is the removeMovieClip() command.

Say you want to be able to create a ton of movieclip clips with custom dynamic depths, but you dont want to worry about removing them when you go to a new frame. You can just create a movieclip to act as a container, and place all your dynamic-depth movieclips in there. Now when you move to a new frame and the movieclip-container dissapears, all the dynamic-depth movieclips will disapear along with it. This is also a very useful thing to use with attachMovie() and duplicateMovieClip().


BBS Signature

Response to AS: Swap Depths Mar 31, 2006


At 3/31/06 11:54 AM, Juice-Tin wrote: When using swapdepths to assign a depth number to an object, the object becomes dynamic (cant think of a better word for it), meaning it can now be removed by the removeMovieClip() command.

nope. that would be incorrect. it is all based on WHERE you send the new clip to. if you assign a depth less than 0 or more than something like 1 million, it is in overflow territory. that is the whole reason you have to swap depths to begin with, because MCs created at authoring time are given negative depths.


BBS Signature

Response to AS: Swap Depths Mar 31, 2006


At 3/31/06 09:58 AM, Tolomus wrote: stuff

You can't have an onClipEvent handler on a button (but you can have an on handler on a movieclip - so I suggest using MCs instead of buttons), so you'll need to stick

mybuttoninstance.swapDepths(depth:Number);

To the frame.


Sup, bitches :)

BBS Signature

Response to AS: Swap Depths May 11, 2006


At 3/31/06 12:11 PM, authorblues wrote:
At 3/31/06 11:54 AM, Juice-Tin wrote: When using swapdepths to assign a depth number to an object, the object becomes dynamic (cant think of a better word for it), meaning it can now be removed by the removeMovieClip() command.
nope. that would be incorrect. it is all based on WHERE you send the new clip to. if you assign a depth less than 0 or more than something like 1 million, it is in overflow territory. that is the whole reason you have to swap depths to begin with, because MCs created at authoring time are given negative depths.

So... If I have a movie clip which has, when it starts, another movie clip inside (in the bottom layer) and hundreds of buttons (in a higher layer) and want to duplicate the movie clip in the bottom layer at run time, with the duplication result appearing UNDER the buttons, what do I do? Do I have to put all buttons inside movie clips and give them instance names? I mean, the next available depth when the clip starts is 0, so I assume the buttons and the rest of the crap that exists when the clip starts are being given negative depth values (I checked with the clip I want to duplicate and it has a depth of like -15000)...

Response to AS: Swap Depths Jul 7, 2006


is there also a way of SETTING the depth?

PS: Example! YAY! (Flash player 8 needed)


I'm one of the people who doesn't have text in his signature.

Response to AS: Swap Depths Jul 7, 2006


At 7/7/06 05:03 PM, Mr_Dark wrote: is there also a way of SETTING the depth?

Well, swapDepths is like setting depths. You can use it in onLoad event to;
onClipEvent(load){
this.swapDepths(1);
}


BBS Signature

Response to AS: Swap Depths Jul 7, 2006


At 7/7/06 05:11 PM, GuyWithHisComp wrote:
At 7/7/06 05:03 PM, Mr_Dark wrote: is there also a way of SETTING the depth?
Well, swapDepths is like setting depths. You can use it in onLoad event to;
onClipEvent(load){
this.swapDepths(1);
}

Yay, thanks. :D


I'm one of the people who doesn't have text in his signature.

Response to AS: Swap Depths Nov 20, 2006


At 3/31/06 09:58 AM, Tolomus wrote:
Is there any way to use this for a Button, and not an MC? This is for a game of mine under construction, but using:

onClipEvent (load) {
this.swapDepths(99999);
}

Just put the button inside of a movie clip, and swapdepths it.

Response to AS: Swap Depths May 12, 2008


**Error** Scene=Scene 1, layer=sfx, frame=686:Line 1: Clip events are permitted only for movie clip instances
onClipEvent (enterFrame) {
it kept sayn that

Response to AS: Swap Depths Aug 10, 2008


can't get this to work:(
want to have the object in another mc...

if (this._y>=_root.mc.object._y-10) {
this.swapDepths(this.getNextHighestDepth ());
}
if (this._y<_root.mc.object._y-10) {
_root.mc.object.swapDepths(this.getNextH ighestDepth());
}

plz help xD im not so good at AS :P