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

Issue with a platformer

H

Half

Guest
This is a code that I got from a youtube video (the step event of obj_player, which is the controllable character):

Code:
/// platform physics



// Check for ground
if (place_meeting(x, y+1, obj_solid)) {
    vspd = 0;
   
    //Jumping
    if (keyboard_check(vk_up)) {
        vspd = -jspd
} 
} else {
    //gravity
    if (vspd < 10) {
        vspd += grav;
    }
}

// Moving right
if (keyboard_check(vk_right)) {
    hspd = spd;
}

// Moving left
if (keyboard_check(vk_left)) {
    hspd = -spd;
}

// Check for not moving
if ((!keyboard_check(vk_right)) && !keyboard_check(vk_left) || !keyboard_check(vk_right) && !keyboard_check(vk_left)) {
    hspd = 0;
}

// Horizontal collisions
if (place_meeting(x+hspd, y, obj_solid)) {
    while (!place_meeting(x+sign(hspd), y, obj_solid)) {
        x+= sign (hspd);
    }
    hspd = 0;
}

//Move horizontally
x += hspd; 

// Vertical collisions
if (place_meeting(x+hspd, y+vspd, obj_solid)) {
    while (!place_meeting(x, y+sign(vspd), obj_solid)) {
        y+= sign (vspd);
    }
    vspd = 0;
}

//Move vertically
y += vspd;
I have a controllable character that can jump around and land on platforms, but when it touches the side of a platform the character freezes in the air and the game crashes.

Is there something that is missing from the code?
 

FrostyCat

Redemption Seeker
I'm certain you didn't get this from the video:
Code:
if (place_meeting(x+hspd, y+vspd, obj_solid)) {
    while (!place_meeting(x, y+sign(vspd), obj_solid)) {
       y+= sign (vspd);
   }
   vspd = 0;
}
If you genuinely understand what this piece of code is for, you should easily see why the +hspd has no business being in that position.

Don't just copy code off a video and think you're done, walk through the code yourself and prove it yourself. Otherwise you aren't learning.
 
H

Half

Guest
Thank you. I'll take your advice on learning what it means as well.
 
Top