G
Grady Boudreaux
Guest
Hey! So I'm making a platformer game, but whenever I try to jump, the player teleports down and to the left. I've looked around a bit but had no idea why this was happening. Hopefully, someone with more experience than me can swoop in and fix the problem. Here's the code:
The Step Event:
//Get Player Input
key_left = keyboard_check(vk_left) or keyboard_check(ord("A"));
key_right = keyboard_check(vk_right) or keyboard_check(ord("D"));
key_jump = keyboard_check_pressed(vk_space) or keyboard_check(vk_up) or keyboard_check(ord("W"));
//Calculate Movement
var move = key_right - key_left;
hsp = move * walksp;
vsp = vsp + grv;
//Jumping
if (place_meeting(x, y + 1, obj_block)) and (key_jump) {
vsp = -7;
}
//Horizontal Collision
if (place_meeting(x + hsp, y, obj_block)) {
while (!place_meeting(x + sign(hsp), y, obj_block)) {
x = x + sign(hsp);
}
hsp = 0;
}
//Vertical Collision
if (place_meeting(x, y + vsp, obj_block)) {
while (!place_meeting(x, y + sign(vsp), obj_block)) {
y = y + sign(vsp);
}
vsp = 0;
}
//Animation / Graphics
if (!place_meeting(x, y + 1, obj_block)) {
sprite_index = spr_player_airborne;
image_speed = 0;
if (sign(vsp) > 0) image_index = 1; else image_index = 0;
}
else {
image_speed = 1;
if (hsp == 0) {
sprite_index = spr_player_two
}
else {
sprite_index = spr_player_running;
}
}
x = x + hsp;
y = y + vsp;
The Create Event:
hsp = 0;
vsp = 0;
grv = 0.3;
walksp = 4;
The Step Event:
//Get Player Input
key_left = keyboard_check(vk_left) or keyboard_check(ord("A"));
key_right = keyboard_check(vk_right) or keyboard_check(ord("D"));
key_jump = keyboard_check_pressed(vk_space) or keyboard_check(vk_up) or keyboard_check(ord("W"));
//Calculate Movement
var move = key_right - key_left;
hsp = move * walksp;
vsp = vsp + grv;
//Jumping
if (place_meeting(x, y + 1, obj_block)) and (key_jump) {
vsp = -7;
}
//Horizontal Collision
if (place_meeting(x + hsp, y, obj_block)) {
while (!place_meeting(x + sign(hsp), y, obj_block)) {
x = x + sign(hsp);
}
hsp = 0;
}
//Vertical Collision
if (place_meeting(x, y + vsp, obj_block)) {
while (!place_meeting(x, y + sign(vsp), obj_block)) {
y = y + sign(vsp);
}
vsp = 0;
}
//Animation / Graphics
if (!place_meeting(x, y + 1, obj_block)) {
sprite_index = spr_player_airborne;
image_speed = 0;
if (sign(vsp) > 0) image_index = 1; else image_index = 0;
}
else {
image_speed = 1;
if (hsp == 0) {
sprite_index = spr_player_two
}
else {
sprite_index = spr_player_running;
}
}
x = x + hsp;
y = y + vsp;
The Create Event:
hsp = 0;
vsp = 0;
grv = 0.3;
walksp = 4;