## Depths + Jumping = shit

• 738 Views
• 3 Replies
jotendo
jotendo
• Member since: Nov. 25, 2005
• Offline.
Forum Stats
Member
Level 09
Blank Slate
Depths + Jumping = shit 2009-06-23 21:32:01

hey,
I've got a problem with depths and jumping.

Basically the depth of the player and enemies is figured by the _y
so if the player is further down the screen he will be infront.

Now that I've added jumping, its all gone to shit because of the _y value changing during the jumping.

I know that I can make the depth changing false if jumping is true, but then that screws up when you jump forward and backward on the Y Axis

Here is an example of my problem:

And the following is the code for the enemy:

movespeed = 3;
movespeed2 = 2;
scale = _xscale;
}
onClipEvent (enterFrame) {
distancex= this._x-_root.player._x;
distancey= this._y-_root.player._y;
if(distancex < 160 && distancex > 70 && distancey < 150){//if the distance between player and enemy is between 160 and 100... (to increase or descrease max and min range, just increase or decrease the numbers)
_xscale = -scale;
_x-=movespeed;//make the enemy walk towards the player. Increase number to increase enemy's speed.
}
if(distancex > -160 && distancex < -70 && distancey < 150){//if the distance between player and enemy is between -10 and -160... (to increase or descrease max and min range, just increase or decrease the numbers)
_xscale = +scale;//turn the enemy in the proper direction
_x+=movespeed;//make the enemy walk towards the player. Increase number to increase enemy's speed.
}
if(distancey < 100 && distancey > 10 && distancex < 40){//if the distance between player and enemy is between 160 and 100... (to increase or descrease max and min range, just increase or decrease the numbers)
_xscale = -scale;
_y-=movespeed2;//make the enemy walk towards the player. Increase number to increase enemy's speed.
}
if(distancey > -100 && distancey < -10 && distancex < 40){//if the distance between player and enemy is between -10 and -160... (to increase or descrease max and min range, just increase or decrease the numbers)
_xscale = +scale;//turn the enemy in the proper direction
_y+=movespeed2;//make the enemy walk towards the player. Increase number to increase enemy's speed.
}
}
onClipEvent (enterFrame) {
thisDepth=this.getDepth(), otherDepth=_root.player.getDepth();
if (_y>_root.player._y && otherDepth>thisDepth) {
this.swapDepths(_root.player);
}
if (_root.player._y>_y && thisDepth>otherDepth) {
this.swapDepths(_root.player);
}
}
this._x=random(550);
this._y=226+random(274);
}

}

And the player code as well:

speed = 3;
speed2 = 2;
y_start = _y;
jumping = false;
jumpspeed = 0;
run= false;
scale = _xscale;
}
onClipEvent (enterFrame) {
if ((Key.isDown(Key.RIGHT))) {
if(jumping==true) {
this.gotoAndStop(5);
} else {
this.gotoAndStop(2);
}
this._x += speed;
run=true;
_xscale = +scale;
}
if ((Key.isDown(Key.LEFT))) {
if(jumping==true) {
this.gotoAndStop(5);
} else {
this.gotoAndStop(2);
}
this._x -= speed;
run=true;
_xscale = -scale;
}
if ((Key.isDown(Key.UP))) {
if(jumping==true) {
this.gotoAndStop(5);
} else {
this.gotoAndStop(2);
}
this._y -= speed2;
y_start-= speed2;
run=true;
}
if ((Key.isDown(Key.DOWN))) {
if(jumping==true) {
this.gotoAndStop(5);
} else {
this.gotoAndStop(2);
}
this._y += speed2;
y_start+=speed2;
run=true;
}

if (Key.isDown(65) && run==false) {
this.gotoAndStop(4);
}
if (jumping) {
_y += jumpspeed;
jumpspeed += 1;
if (_y>=y_start) {
_y = y_start;
jumping = false;
this.gotoAndPlay(1);
_root.jump=false;
}
} else {
if (Key.isDown(83)) {
jumping = true;
jumpspeed = -10;
_root.jump=true;
this.gotoAndStop(5);
}
}
}
onClipEvent(keyUp) {
run=false;
if(jumping==false) {
this.gotoAndPlay(1);
}
}

Thanks!!

Coolio-Niato
Coolio-Niato
• Member since: Jun. 30, 2005
• Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to Depths + Jumping = shit 2009-06-23 22:41:57

Didnt read the code. next time dont post all of it.

I do though know what you have to do. Set a variable that tracks the y position on the screen. Set another one that tracks the jump height at that moment. Let the character's y position be a combination of the two (ypos+jumpPos), but the depths depend only on the first variable (ypos).

Ez.

jotendo
jotendo
• Member since: Nov. 25, 2005
• Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to Depths + Jumping = shit 2009-06-23 22:47:44

At 6/23/09 10:41 PM, Coolio-Niato wrote:

::

okay cool
will that work if I jump backwards over an enemy? or diagonal

Coolio-Niato
Coolio-Niato
• Member since: Jun. 30, 2005
• Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to Depths + Jumping = shit 2009-06-24 14:27:52

At 6/23/09 10:47 PM, jotendo wrote:
At 6/23/09 10:41 PM, Coolio-Niato wrote:
okay cool
will that work if I jump backwards over an enemy? or diagonal

yes.