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

GameMaker Can't get my player sprite to move horizontally

M

mikke

Guest
So I'm SUPER new to GMS2, and all game programming really, so I'm probably making a rookie mistake. I'm following Shaun Spalding's complete platformer tutorial and have copied the code character for character to add movement to the player object, albeit with adjustments for the numbers in his jump/fall animation (it had 6 frames, but I chose 2 of them for the tutorial and my own sake) and variable names. I have the animation for walking set as well, to a speed of 12. However, with this code, I've found that my sprite can't move horizontally unless he's airborne. He can jump horizontally and the animations for jumping work just fine -- if I wanted to make a platformer where the only movement was jumping (before mirroring the sprite) I'd be fine. But on the ground, he's stuck in one spot as his idle sprite. I cannot for the life of me figure it out as I've done the same thing he did, at least as far as I'm aware. I could be having a tunnel vision moment.

GML:
// get player input
key_left = keyboard_check(vk_left);
key_right = keyboard_check(vk_right);
key_jump = keyboard_check_pressed(vk_up)

//calculate movement
var move = key_right - key_left;

hsp = move * walksp;

vsp = vsp + grv;

if (place_meeting(x,y+1,oWall)) and (key_jump)
{
    vsp = -7;
}

//horizontal collision
if (place_meeting(x+hsp,y,oWall))
{
    while (!place_meeting(x+sign(hsp),y,oWall))
    {
        x = x + sign(hsp);
    }
    hsp = 0;
}
x = x + hsp;

//vertical collision
if (place_meeting(x,y+vsp,oWall))
{
    while (!place_meeting(x,y+sign(vsp),oWall))
    {
        y = y + sign(vsp);
    }
    vsp = 0;
}
y = y + vsp;


//animation
if (!place_meeting(x,y+1,oWall))
{
    sprite_index = sPlayerJump;
    image_speed = 0;
    if (sign(vsp) > 0) image_index = 2; else image_index = 4;
}
else
{
    image_speed = 1;
    if (hsp == 0)
    {
        sprite_index = sPlayer;
    }
    else
    {
        sprite_index = sPlayerWalk;
    }
}
I have to be missing something but I can't seem to figure it out, despite putting my GMS2 next to his video to compare the code side-by-side. I found that the problem begins here:

GML:
else
{
    image_speed = 1;
    if (hsp == 0)
    {
        sprite_index = sPlayer;
    }
}
I've double and triple and quadruple and beyond checked with Shaun Spalding's video, it looks the same to me. I could be wrong, however. Any help would be much appreciated!
 
M

mikke

Guest
Are you sure all of the sprites have the same bounding box and origin?
Just edited the sprites to have the same bounding boxes, and it fixed the horizontal movement problem, but now my jump/fall animation is inappropriate as the player object touches the ground (it looks to be having a hard time switching between the jump and idle sprites) and any horizontal movement doesn't last for long because he keeps getting stuck in the walls. Sigh.
 
M

mikke

Guest
UPDATE:
Did everything I could and nothing seemed to work for my character getting stuck once the animation was working -- I decided to start a new project and do everything I did in the first one the exact same way, literally by copying the code, and it's working just fine in this project. Interesting.
 
Top