Depth Problems

Ok, im working on random things here and there when i’m bored and not on Wow (World of Warcraft). I have been trying to make it so when the characters _y > enemies _y then the characters depth is les than the enemies, etc.
I have attached a swf to show what I mean. When the char is above the enemy, I want him to appear behind him. And the opposite when hes in front. Get it?
(Btw, there are alot of things in this swf that I have not added yet. Wall collision, enemy facing correct direction, etc.)
The code I was trying is the following.


onClipEvent(enterFrame){
 if(this._y>_root.mainchar._y){
  _root.mainchar.getNextHighestDepth();
 }else if(this._y<_root.mainchar._y){
  this.getNextHighestDepth();
 }
}

Apparently I guess I don’t get what i’m doing wrong.

EDIT: Oh, btw. The zombie is to the bottom left of the room.

You want swapDepths. In that case the code would be:


onClipEvent(enterFrame){
 if(this._y>_root.mainchar._y){
  _root.mainchar.swapDepths(_root.getNextHighestDepth());
 }else if(this._y<_root.mainchar._y){
  this.swapDepths(_root.getNextHighestDepth());
 }
}

That should do the trick. You might want to switch the getNextHighestDepths to fixed depths, because they could quickly add up to something out of bounds.

Awesome, didn’t know till now about swapDepth. But now it leads into another prob which I can’t seem to get around. I’v attached a new version of the swf. to show whats happening now. Because of the zombie getting the next highest depth, his depth is even higher than the masked circle that is supposed to hide him. I tried using the swapDepth on the view too, but then the mask just disappeared. Any ideas?


onClipEvent (enterFrame) {
    _root.mainchar._y -= 6;
    _root.mask.swapDepths(3)
    if (this._y>_root.mainchar._y) {
        this.swapDepths(2);
        _root.mainchar.swapDepths(1);
    } else if (this._y<_root.mainchar._y) {
        _root.mainchar.swapDepths(2);
        this.swapDepths(1);
    }
}

What about something like that?

Yes, I agree, fixed depths would be the way to go in this case. The circle that’s supposed to hide him can be three, and the zombie and the character can be one or two respectively

Yea I see how that would work. Should be fine. I’ll give it a shot in a bit. If it doesn’t work then i’ll repost, but I think it should work fine. :cowboy:

The problem with fixwed depths, it you have to make sure your not using the same depth twice in a scene, or the new one will overwrite the old. For example, in my game I generate the levels by drawing it with AS and giving each line a depth between 1 and 200. I ran into a problem where I was sharing the 200 slot with one of my attacks. If enough lines were generated, you could remove one accidently by using the attack. Just something to keep in mind.

Why not just:
ActionScript Code:
[FONT=Courier New][LEFT][COLOR=#0000FF]_root[/COLOR].[COLOR=#000080]mainchar[/COLOR].[COLOR=#0000FF]swapDepths[/COLOR]COLOR=#000000[/COLOR];
[/LEFT]
[/FONT]

You might need to do a Math.round() for the _root.mainchar._y, I’m not sure.

Btw, that code needs to be executed everytime the character moves in the y direction. You could have it execute everyframe, but that isn’t very optimized.
Also, computer opponents need to run the same scripts but instead of _root.mainchar it should be whatever their instance name is.

Okay, whenever I change the depth of the character and the zombie two problems occur.
The first problem is if I don’t put in a code for the view MC mask, then it appears behind both characters.
The second problem is if I do put in a code, the view MC mask disappears
[COLOR=black][FONT=Arial]entirely.[/FONT][/COLOR]
[COLOR=black][FONT=Arial]Here is the code I am using for the enemy MC which has the depth perception. [/FONT][/COLOR]
[COLOR=black][FONT=Arial]

[/FONT][/COLOR]
[COLOR=black][FONT=Arial]//enemy movement
onClipEvent(enterFrame){
 if(this.hitTest(_root.view)){
  if(this._x>_root.mainchar._x){
   this._x-=5;
  }else if(this._x<_root.mainchar._x){
   this._x+=5;
  }
  if(this._y>_root.mainchar._y){
   this._y-=5;
  }else if(this._y<_root.mainchar._y){
   this._y+=5;
  }
 }
}
//Depth preception
onClipEvent(enterFrame){
 if(this._y<_root.mainchar._y){
  _root.mainchar.swapDepths(_root.getNextHighestDepth());
 }else if(this._y>_root.mainchar._y){
  this.swapDepths(_root.getNextHighestDepth());
 }
}[/FONT][/COLOR]
[COLOR=black][FONT=Arial]

[/FONT][/COLOR]
[COLOR=black][FONT=Arial]I tried usig the swapDepths(2); as suggested above. but it did the second problem.[/FONT][/COLOR]

Ok. First off, there is no need to have two on enterFrames. I ususally only have one enterFrame calling a bunch of functions, but that will come as you work more with flash.

One really quick fix is to just get rid of the second enterFrame and put the code inside into the end of the first enterFrame, like this:

 onClipEvent(enterFrame){
 //enemy movement
 if(this.hitTest(_root.view)){
  if(this._x>_root.mainchar._x){
   this._x-=5;
  }else if(this._x<_root.mainchar._x){
   this._x+=5;
  }
  if(this._y>_root.mainchar._y){
   this._y-=5;
  }else if(this._y<_root.mainchar._y){
   this._y+=5;
  }
 }

 //Depth preception
 if(this._y<_root.mainchar._y){
  _root.mainchar.swapDepths(_root.getNextHighestDepth());
 }else if(this._y>_root.mainchar._y){
  this.swapDepths(_root.getNextHighestDepth());
 }
} 

If you are planning on adding multiple enemies, I wouldn’t suggest doing the swapDepth like that. You should consider this:

 onClipEvent(enterFrame){
 //enemy movement
 if(this.hitTest(_root.view)){
  if(this._x>_root.mainchar._x){
   this._x-=5;
  }else if(this._x<_root.mainchar._x){
   this._x+=5;
  }
  if(this._y>_root.mainchar._y){
   this._y-=5;

   //Depth preception
   this.swapDepths(this._y);

  }else if(this._y<_root.mainchar._y){
   this._y+=5;

   //Depth preception
   this.swapDepths(this._y);
  }
 }
} 

And on the character movement code where you have the Key.isDown:

 if (Key.isDown(Key.UP)) {
 //movement code here

 //Depth preception
 this.swapDepths(this._y);
 //if the code isn't on the character MC, replace the path of "this" with the path of the character
} else if (Key.isDown(Key.DOWN)) {
 //movement code here
		
 //Depth preception
 this.swapDepths(this._y);
 //if the code isn't on the character MC, replace the path of "this" with the path of the character
} 

Hope that helps.

-DBarbarian

Swapping depths with the _y value of the characters seems like a really bad idea to me. While it would work, if you have another MC at the same depth, the MC either: 1) won’t load, because it can’t remove the MC at that depth or 2) will replace the MC at that depth. This might cause errors such as enemies don’t spawn in a room, missing walls, missing MC’s etc.

With fixed depths, make the mask’s depth higher then everything else. Make it like 10, and everything you want hidden 0-9. That way everything is always hidden. Also, when your swapping depths, it might be a good idea to use 3 depths. Swap the enemy from 1 to 3, and keep the Character at 2.

I don’t think you know how swapDepths works then.

swapDepths either swaps the MC to the specified depth, and if there is a movie clip occupying that depth, it would swap the depth with that MC.

Works fine now using the codes posted above, to a certain extent. I’v attached a swf. file to show how it runs.

Ok, i’v attached the fla. version for people who want to try and do something with it, or want to use whats already there.
Oh and by the way, the reason I had the on(enterFrame){ twice was because it my personal way of seperating information to organize things. So I know where they are. I don’t mind looking through large scripts if I know where everything is. :beam:

lol, forgot to post the file. :lol:

Well, if you really want to sacrifice a lot of performance (especially if you want to add more enemies) for the sake of seeing the code better, then that is your choice.
Glad you got it to work though.