2
\$\begingroup\$

EDIT: I changed the origin of the run sprite to the center, and it is no longer hovering above the jumping sprite. It now flashes in the same location as the run sprite when moving to the side. Everything else is the same.

When I move side to side, the jump sprite moves along the ground while a single frame of the running sprite animation shows up above it and to the side. Both of the sprites flash when this happens. While moving sideways, the sprite slowly sinks into the floor. Jumping works just fine.

I'm very new to game maker and programming in general, so I'm probably making some dumb mistakes. Everything was working fine before I added the "animate" section. Here's the code I'm using:

//get player input
key_right = keyboard_check(vk_right);
key_left = -keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_space);

//react to imput
move = key_right + key_left;

hsp = move * movespeed;

if (vsp < 10) vsp += grav;

if (place_meeting(x,y+1,obj_wall))
{
    vsp = key_jump * -jumpspeed
}

//horizontal collisions
if (place_meeting(x+hsp,y,obj_wall))
{
    while(!place_meeting(x+sign(hsp),y,obj_wall))
    {
        x += sign(hsp);
    }

    hsp = 0;
}

x += hsp;

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

y += vsp;

//animate

if (move!=0) image_xscale = move;

if (place_meeting(x,y+1,obj_wall))
{
    if (move!=0) sprite_index = spr_player_run; else sprite_index = spr_player_idle;
}
//else
//{
   if (vsp < 0) sprite_index = spr_player_jump //; else sprite_index = spr_player_fall;

   if (vsp > 0) sprite_index = spr_player_jump
//}
\$\endgroup\$
1
\$\begingroup\$

I haven't tested the code but I can foresee one possible problem. Once you change the sprite to spr_player_jump you don't change it to spr_player_idle or spr_player_run until there is a place meeting at x,y+1, obj_wall.

Normally this might be okay but if your origin is at the center and not the bottom of the sprite's feet it won't trigger. The x,y location looks at the exact point of the origin rather than the whole sprite.

While you're working with this, try something that might help with debugging. Turn your sprite changes into a series of else ifs and have a catch all else statement at the bottom.

if(place_meeting(x,y+1,obj_wall)){
  //logic to change sprite to run or idle
}
else if (vsp<0 or vsp>0){
  //logic to change to jump
}
else{
  /*have some kind of debugging info here to detect if none of the above conditions are 
  being satisfied. Maybe turn the sprite into some other sprite completely so you know
  something went wrong. That would help know what part is causing the problem.*/
}
\$\endgroup\$
1
  • \$\begingroup\$ You could simply write if (vsp==0) {...} else {...} instead of if (vsp<0 or vsp>0) {...} else {...} and switching the related statements each other \$\endgroup\$ – liggiorgio Apr 5 '16 at 20:49
1
\$\begingroup\$

Your code is fine as is, maybe some lines can be written better and optimizations are possible. But it does work. The reason your character animation glitches is because of the related collision mask.

When your character runs, his legs move and - right like in real life - there's a brief moment when both of them aren't touching the ground. Because of the precise collision mask your sprites have, there will be a step in which your code will be checking for collisions below (place_meeting(x,y+1,obj_wall)) when your character actually isn't touching the ground in position (x,y+1), assuming then he's jumping/falling.

So, when moving downwards because of gravity, your character will change animation from falling to idling/running, and the glitch repeats.

The solution to your problem is simple: change the collision mask of the sprites you're using (a custom shape is better than "automatic"), or even better use a mask sprite for your obj_player so that changing sprites won't affect the collision check: if you run and then stop because a wall, there shouldn't be more space to walk further!

\$\endgroup\$
0
\$\begingroup\$

Make sure that your spr_player_idle is not middle centred, as this might cause an issue of going through the ground.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.