Be a Supporter!

AS: Swap Depths

  • 8,169 Views
  • 27 Replies
New Topic Respond to this Topic
liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
AS: Swap Depths Aug. 19th, 2005 @ 11:45 AM Reply

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
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Swap Depths Aug. 19th, 2005 @ 11:47 AM Reply

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.

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: Swap Depths Aug. 19th, 2005 @ 11:51 AM Reply

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
T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to AS: Swap Depths Aug. 21st, 2005 @ 08:15 AM Reply

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.

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: Swap Depths Aug. 21st, 2005 @ 08:16 AM Reply

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
Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to AS: Swap Depths Aug. 23rd, 2005 @ 11:27 AM Reply

can a depth have a decimal?

T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to AS: Swap Depths Aug. 23rd, 2005 @ 11:28 AM Reply

---no

noob-toast
noob-toast
  • Member since: May. 18, 2004
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: Swap Depths Aug. 23rd, 2005 @ 11:30 AM Reply

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
Afro-Ninja
Afro-Ninja
  • Member since: Mar. 2, 2002
  • Offline.
Forum Stats
Moderator
Level 44
Game Developer
Response to AS: Swap Depths Aug. 23rd, 2005 @ 12:26 PM Reply

Depths are always whole numbers


BBS Signature
IWantSomeCookies
IWantSomeCookies
  • Member since: Aug. 20, 2004
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to AS: Swap Depths Oct. 21st, 2005 @ 07:25 PM Reply

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

FlyingPika
FlyingPika
  • Member since: Oct. 30, 2003
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to AS: Swap Depths Dec. 11th, 2005 @ 04:15 AM Reply

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!

authorblues
authorblues
  • Member since: Jun. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Swap Depths Dec. 11th, 2005 @ 04:18 AM Reply

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
FlyingPika
FlyingPika
  • Member since: Oct. 30, 2003
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to AS: Swap Depths Dec. 11th, 2005 @ 04:21 AM Reply

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

IWantSomeCookies
IWantSomeCookies
  • Member since: Aug. 20, 2004
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to AS: Swap Depths Dec. 11th, 2005 @ 05:01 AM Reply

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

FlyingPika
FlyingPika
  • Member since: Oct. 30, 2003
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to AS: Swap Depths Dec. 11th, 2005 @ 06:54 AM Reply

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!

Tolomus
Tolomus
  • Member since: Nov. 17, 2005
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Swap Depths Mar. 31st, 2006 @ 09:58 AM Reply

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.

Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to AS: Swap Depths Mar. 31st, 2006 @ 10:03 AM Reply

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
Tolomus
Tolomus
  • Member since: Nov. 17, 2005
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Swap Depths Mar. 31st, 2006 @ 10:54 AM Reply

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...

Juice-Tin
Juice-Tin
  • Member since: Feb. 3, 2001
  • Offline.
Forum Stats
Member
Level 09
Game Developer
Response to AS: Swap Depths Mar. 31st, 2006 @ 11:54 AM Reply

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
authorblues
authorblues
  • Member since: Jun. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Swap Depths Mar. 31st, 2006 @ 12:11 PM Reply

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
liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: Swap Depths Mar. 31st, 2006 @ 12:17 PM Reply

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
Protected
Protected
  • Member since: Aug. 29, 2003
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: Swap Depths May. 11th, 2006 @ 10:17 AM Reply

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)...

Mr-Dark
Mr-Dark
  • Member since: Dec. 7, 2004
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to AS: Swap Depths Jul. 7th, 2006 @ 05:03 PM Reply

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.

GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS: Swap Depths Jul. 7th, 2006 @ 05:11 PM Reply

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
Mr-Dark
Mr-Dark
  • Member since: Dec. 7, 2004
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to AS: Swap Depths Jul. 7th, 2006 @ 05:58 PM Reply

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.

Isledeavarice
Isledeavarice
  • Member since: Aug. 15, 2001
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Swap Depths Nov. 20th, 2006 @ 02:55 PM Reply

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.

beast-unleashed
beast-unleashed
  • Member since: Oct. 17, 2007
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Swap Depths May. 12th, 2008 @ 04:07 PM Reply

**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

nwalZ
nwalZ
  • Member since: Aug. 9, 2008
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to AS: Swap Depths Aug. 10th, 2008 @ 07:04 AM Reply

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