• 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!

Z axis in 2D jumping on a platform. Help!

Hello, I am currently trying to make my player object be able to jump and be able to stay on platforms. I am not using physics btw. Anyway, I found a video that explains this concept but I am struggling to implement this on my project because my depth and collision system is different. This is the video:

So, the code that I am using for this project is as follows:

Player (obj_player)

CREATE EVENT
event_inherited(); //The player is a child of obj_collision. I will explain this after.

image_speed = 0.5;

//Movement speed Variables
wspd = 2;
rspd = 5;
spd = wspd;

I am just basically using "spd" for the movement.

//Variables for Jumping
z = 0;
zfloor = 0;
moveZ = 0;
jspd = 3;
grav = 0.2;

//Horizontal and Vertical Movement Variables

moveX = 0;
moveY = 0;

I use these variables to update the x and y position of the player combining it with the "spd" variable.


STEP EVENT
//Keys for movement
input_right = keyboard_check (vk_right);
input_left = keyboard_check (vk_left);
input_up = keyboard_check (vk_up);
input_down = keyboard_check (vk_down);
input_run = keyboard_check (ord("Z"));
input_jump = keyboard_check_pressed (vk_space);

//If we press Z, player runs (This just makes the player run, it is not so important for this.)
if (input_run){
spd = rspd;
} else {
spd = wspd;

}

//Intended Movement and Speed
moveX = (input_right - input_left) * spd; //When only the right key is held, it will make 1 positive. While, left makes 1 negative.
moveY = (input_down - input_up) * spd; //When only the down key is held, it will make 1 positive. While, up makes 1 negative.

Like I said before, this updates the x and y position of the player. It is multiplied by spd, so it gives us certain speed. Later, whatever result moveX and moveY is, it will be added to the x and y of the player.

//Jump
if (z > 0) { //In Air
moveZ -= grav;
z += moveZ;
if (moveZ > 0 and !input_jump_hold) {
moveZ = 0;
}
} else {
z = 0;
if (input_jump) {//Jump
moveZ = jspd;
z = moveZ;
} else {
moveZ = 0;
}
}

//Collision Checks

//Horizontal Collision
if(moveX != 0)
{
var collisionH = instance_place(x + moveX, y, obj_collision);
if(collisionH != noone and collisionH.collideable) {
repeat(abs(moveX)){
if(!place_meeting(x+sign(moveX), y, obj_collision))
{
x += sign(moveX);
} else {
break;
}
}
moveX = 0;
}
}


//Vertical Collision
if(moveY != 0)
{
var collisionV = instance_place(x, y + moveY, obj_collision);
if(collisionV != noone and collisionV.collideable) {
repeat(abs(moveY)){
if(!place_meeting(x, y+sign(moveY), obj_collision))
{
y += sign(moveY);
} else {
break;
}
}
moveY = 0;
}
}

Btw, I am using "repeat", "abs" and "sign" functions on my player´s moveX and moveY to collide perfectly well with other objects. Basically, a pixel perfect collision (no spaces between).



//Apply Movement to the Player
x += moveX; //Player x movement according to whatever moveX is
y += moveY - moveZ; //Player y movement according to whatever moveY is

Btw, I put "moveZ" there because this will give us the illusion of the jump (substracting the y position). I also added "z" in my depth system, so it works as a "z" axis. I am explaining this later.

DRAW EVENT

//Draw Player Shadow
draw_sprite (spr_shadow, 0, x, y+z); //I added the "z" on the spr_shadow, so it won´t follow the player sprite whenever we jump.

//Draw Player
draw_sprite(sprite_index, image_index, x, y);

Alright, now I want to explain the Depth and Collision System that I have. So, I have created an object called "par_depthobject", it is used as list for the depth system and it is parent of "obj_collision". obj_collision is parent of obj_player and the rest of the objects that should have collision. With this method, par_depthobject will use all the instances of obj_collision for the list. And this list will be used for the Depth system, which is managed by another object called "depthsorter"

depthsorter
CREATE EVENT
ds_depthgrid = ds_grid_create(2, 1);

DRAW EVENT
//Gets the number of instances that exist
var inst_num = instance_number(par_depthobject); //I am using a local variable to call the "list", which is par_depthobject.

//The Depth Grid
var dgrid = ds_depthgrid;

//Resizes the grid if an instance is created or destroyed
ds_grid_resize(dgrid, 2, inst_num); //So, it is gonna resize according to the amount of instances "inst_num"(par_depthobject) currently has.

//Add instances to the grid
var yy = 0; with (par_depthobject) {
dgrid[# 0, yy] = id;
dgrid[# 1, yy] = y+z; //I add "z" because, when we are jumping, we have to take the "z" in consideration with the "y" axis
yy += 1;
}

//Sort the grid in Ascending order
ds_grid_sort(dgrid, 1, true);

//Loop through the grid and Draw all instances
var inst; yy = 0; repeat(inst_num) {
//Pull out and id
inst = dgrid[# 0, yy];

//Get instance to Draw Itself
with(inst) {
event_perform(ev_draw, 0); //Depthsorter will manage all draw events of our instances, so the depth is correct.
}

yy += 1;
}

CLEAN UP EVENT
ds_grid_destroy(ds_depthgrid);

ROOM START EVENT
layer_add_instance("Instances", id);

So, this is difference...

On the video, he manages his obj_player´s movement using the Key Events.
For example, when he uses the Key Event (using the "A" key), he will move to the left and inside the EVENT, he writes this:

o_player

KEY EVENT
///move left
//update instleft
instleft = noone;
//instance in path
instleft = (instance_place(x-spd,y,o_block_par))
if (!instleft) or (instleft.z <= z)
{
x -= spd;
}

What he is doing is: if o_block_par is not there or if o_block_par´s z position is less or equal to the player´s z pos, then the player will move to the left.

And he uses the same for the other directions.

Then, he uses the COLLISION EVENT

COLLISION EVENT
///update zfloor
instbelow = instance_place(x,y,o_block_par);
zfloor = instbelow.z

I have tried to replicate that in my code but I have failed many times to the point where I don´t know what I am doing wrong. How can I make this but without using the COLLISION EVENT? Because, I manage the collision in the STEP EVENT of my obj_player as I explained before. I also, dont want to have my movement wraped around the KEY EVENTS because it will limit some other stuff that I am currently working on.
 
Top