Need help understanding swapDepths();

Got a question about sprites, code, or anything concerning about the development of your game? Find the know-how here.
Post Reply
User avatar
AvianDF
Lurker
Posts: 8
Joined: July 4th, 2010, 7:11 pm
Contact:

Need help understanding swapDepths();

Post by AvianDF » March 10th, 2011, 11:36 pm

I know it's been quite a while since I last posted something on here. Lately I've been on an information binge and have been learning a lot about flash in the past couple days. I've hit a wall though and since you all have a nice community here this seems like a good place to post this question. Currently I've been learning how to create an action RPG. I have a lot of the mechanics I need figured out allowing me to create this:



However I'm currently at a loss on dealing with the function swapDepth. The effect I'm trying to achieve is having the hero appear in front of the rabite when he's standing below it, and having the hero appear behind the rabite when he's above it. While it works for one rabite I can't for the life of me figure out how to get it to work for more then one rabite on the same screen. This is the current code I have set for the two rabites:

Code: Select all

onClipEvent (load) {
	bposition = 0;
}

onClipEvent (enterFrame) {
	if (this._y>=_root.Player._y && bposition == 0) {
		bposition = 1;
		this.swapDepths(_root.Player);
	} else if (this._y<=_root.Player._y && bposition == 1) {
		bposition = 0;
		this.swapDepths(_root.Player);
	}
}
I could make separate codes for each rabite but I need to be practical and develop a one size fits all not just for enemies but NPC's as well. Does anyone have any tips or tricks that might come in handy? Thank you in advance ^v^

User avatar
humbird0
Tentacle Swarm
Tentacle Swarm
Posts: 374
Joined: June 29th, 2009, 8:50 pm
Location: NY State
Contact:

Re: Need help understanding swapDepths();

Post by humbird0 » March 11th, 2011, 8:04 am

I have some code that does exactly that:
http://humbird0.net63.net/misc/my_flash ... 1-2011.zip

I have to go to work right now, so I don't have time to explain it.
Just open the file for instructions.
You need to put all your sprites inside of a single movieClip, and then tell this code what movieClip to affect.
It'll do the sorting automatically from that point on.

User avatar
AvianDF
Lurker
Posts: 8
Joined: July 4th, 2010, 7:11 pm
Contact:

Re: Need help understanding swapDepths();

Post by AvianDF » March 11th, 2011, 6:21 pm

humbird0 wrote:I have some code that does exactly that:
http://humbird0.net63.net/misc/my_flash ... 1-2011.zip

I have to go to work right now, so I don't have time to explain it.
Just open the file for instructions.
You need to put all your sprites inside of a single movieClip, and then tell this code what movieClip to affect.
It'll do the sorting automatically from that point on.
Thank you for your reply. I've been looking over the code but I'm kind of at a loss of how to implement it properly. Hopefully we can have a chance to chat as I've very interested to see how to use this code. *Snugs* Thank you again for your reply, I appreciate it ^v^

User avatar
humbird0
Tentacle Swarm
Tentacle Swarm
Posts: 374
Joined: June 29th, 2009, 8:50 pm
Location: NY State
Contact:

Re: Need help understanding swapDepths();

Post by humbird0 » March 12th, 2011, 7:37 pm

- How to use it -
Here's how you can use this without changing your game too much.
Create an empty object, like this:

Code: Select all

_global.SPRITES = {};
SPRITES._name = "any text";
Copy the autoSort.as file to the same folder as your game, and add these lines somewhere in the game.
I recommend putting it right under the code above that creates the SPRITES object.

Code: Select all

#include "autoSort.as"
addAutoSort( SPRITES );
Finally, have each character add a reference to themselves in that object.
In other words, paste this code inside of the hero, rabite, and any other characters that you want to get sorted:

Code: Select all

SPRITES[_name] = this;
I think it'll work.

User avatar
humbird0
Tentacle Swarm
Tentacle Swarm
Posts: 374
Joined: June 29th, 2009, 8:50 pm
Location: NY State
Contact:

Re: Need help understanding swapDepths();

Post by humbird0 » March 12th, 2011, 7:38 pm

- How it works -
There are two ways to use the swapDepth() function.
You can either give it the name of a movieClip to swap with.
Or you can give it a depth number to use. (depths are like layers)
My code uses the 2nd method.

Now, what if you had every character constantly change their own depth to be equal to their _y position.
The result is that characters below would appear in front of characters above.

My code does this, but also allows more than one sprite to be on the same _y.
It does this by skipping ahead 10 depth layers for each _y pixel,
and by checking whether or not another sprite already exists on the intended depth.
If one is found, it switches to the next depth up, and checks again until it finds a free spot.

User avatar
humbird0
Tentacle Swarm
Tentacle Swarm
Posts: 374
Joined: June 29th, 2009, 8:50 pm
Location: NY State
Contact:

Re: Need help understanding swapDepths();

Post by humbird0 » March 12th, 2011, 8:01 pm

- What is the code above doing? -
_global.SPRITES = {};
This code creates an empty object that can be accessed globally.
This means that no matter where you are in the game's code, you will always be able to access this object by typing the word: SPRITES.

SPRITES._name = "any text";
I added this extra bit to keep the sorting function from automatically deleting itself.
Normally, when I use the autosorter, I make SPRITES a movieclip instead of an empty object.
And all movieclips have a _name variable.
My autosorter tries to detect when the movieClip has been deleted by seeing whether or not the _name variable is empty. If it can't see the name of this movieClip it's sorting, it assumes the movieClip has been removed and deletes itself.
This piece of code prevents that.


#include "autoSort.as"
The #inclulde statement tells Flash to paste the code in the autoSort.as file right here.
You'll never see it because Flash only does this when it's making the SWF file.
The SWF file will have the sorting code included in it. Your FLA will not.

addAutoSort( SPRITES );
This is a function that's defined inside the autoSort.as file.
By calling it, you're telling the sorter to automatically organize all the movieClips inside of SPRITES.


SPRITES[_name] = this;
This creates a variable inside of the SPRITES object. The variable will have the same name as _name.
And the variable will refer to this, which is the movieClip where this code is.
In other words, the variable this creates, can be used as a shortcut to access this character's movieClip.

This works because when you set a variable equal to a movieClip or an object like so:
myVariable = myMovieClip;
That will not make a copy of the movieClip. It will create a shortcut to it.
So changing myVariable._y will directly affect myMovieClip's _y variable.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests