• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Y variable changing? Struggling with jump feature.

K

KC Frye

Guest

I want to be able to jump and land in the same spot. However, once in the air, my Y variable decreases so I end up lower than before? Please help.

Create Event:
SPD = 3 //Walk speed
collisionSPD = SPD + 2;
vsp = 0; //vertical speed
grv= 1; //gravity
currentY = undefined;
facing = 1;
//facing 0 = front
//facing 1 = back
//facing 2 = left
//facing 3 = right
attacking = false;
guarding = false;
dodging = false;
jumping = false;
Step Event:
key_jump = keyboard_check_pressed(vk_space);
//JUMPING
vsp = vsp + grv;
if (key_jump) and (!jumping){
currentY = y;
vsp = -20;
}
if (jumping = true) {
if (y != currentY - 100){
//rising
y = y + vsp;
}
else {
if (y != currentY){
y = y + vsp;//falling equation - this makes us fall
}
}
}
else {
y = y; //stationary
}
//running
if keyboard_check(vk_shift)
{SPD = 6}
else
{SPD = 3}
//Left (2)
if (keyboard_check(ord("A")) && place_free(x - collisionSPD, y)) {
x -= SPD;
image_speed = SPD / 3;
sprite_index = spr_playerLeft;
facing = 2;
}
//right (3)
if (keyboard_check(ord("D")) && place_free(x + collisionSPD, y)) {
x += SPD;
image_speed = SPD / 3;
sprite_index = spr_playerRight;
facing = 3;
}
//up (0)
if (keyboard_check(ord("W")) && place_free(x, y - collisionSPD)) {
y -= SPD;
image_speed = SPD / 3;
sprite_index = spr_playerBackwards;
facing = 0;
}
//down (1)
if (keyboard_check(ord("S")) && place_free(x, y + collisionSPD)) {
y += SPD;
image_speed = SPD / 3;
sprite_index = spr_playerForwards;
facing = 1;
}
if (attacking = false){
if (keyboard_check(vk_nokey)) {
image_speed = 0;
image_index = 0;
}
}
//Jump
if (keyboard_check_pressed(vk_space)) {
attacking = true;
jumping = true;
switch (facing) {
case 0:
image_speed = 1.5
sprite_index = spr_playerJUMPBackwards;

break;

case 1:
image_speed = 1.5
sprite_index = spr_playerJUMPForwards;

break;

case 2:
image_speed = 1.5
sprite_index = spr_playerJUMPLeft;


break;

case 3:
image_speed = 1.5
sprite_index = spr_playerJUMPRight;

break;

}
}
End Animation Event:
//JUMPING
if (jumping = true) {
switch (facing){
case 0:
image_speed = 0
image_index = 0;
sprite_index = spr_playerBackwards;
attacking = false;
jumping = false;
break;
case 1:
image_speed = 0
image_index = 0;
sprite_index = spr_playerForwards;
attacking = false;
jumping = false;
break;
case 2:
image_speed = 0
image_index = 0;
sprite_index = spr_playerLeft;
attacking = false;
jumping = false;
break;
case 3:
image_speed = 0
image_index = 0;
sprite_index = spr_playerRight;
attacking = false;
jumping = false;
break;
}

}
 

Slyddar

Member
Please use the [ code ] blocks to encapsulate any code as it makes it easier to read and assist you.
Code:
//JUMPING
vsp = vsp + grv;
if (key_jump) and (!jumping){
currentY = y; 
vsp = -20;
}
if (jumping = true) {
if (y != currentY - 100){
//rising
y = y + vsp;
}
else {
if (y != currentY){
y = y + vsp;//falling equation - this makes us fall
} 
}
}
else {
y = y; //stationary
}
You are checking for an exact value, when it may never be that value. Instead check for <= and >=. Also have a think about a line like this, and if it is required: y = y
 
Top