Jump to content
Nirwanda

Can't implement a walking cycle

Recommended Posts

It's been a while since I've used Game Maker. Got distracted from it by rm competitions and whatnot. Anyway back then I was starting out with spriting and decided to try my hand at making a walking and jumping animation cycle. I sort of succeded (given my limited skills) but I was never able to implement it into GM.

At first, I tried to follow a tutorial. But that tutorial used a simpler sprite sheet than I did. I thought my basic knowledge of GML would allow me to fill in the gaps. I modified...and then modified some more and ended up with a spaghetti of code that I could not make neither heads nor tails of. The movement I wanted to implement was idle, run and jump, both to left and right, obviously. Using this godawful sprite. https://www.dropbox.com/s/9bde8p4fy7g28rj/images.rar?dl=0

I'll post the me mess of Game Maker code I used, but mostly for the lulz. Truth is, this doesn't work: don't try this at home, kids.

Create event:

 

 

image_speed = 0.2;
grav = 0.2;
hsp = 0;
vsp = 0;
jumpspeed = 8;
movespeed = 8;
friction = 0.5;
global.direction=1

 

 

Step event:

 

 

key_jump = keyboard_check_pressed (ord("Z"));

if (place_meeting (x,y+1,obj_ground))
{
    if keyboard_check (vk_right)
    {hspeed +=1
    global.direction = 1  
    sprite_index = spr_runR}
    else
    if hspeed >=8 hspeed =8
    if keyboard_check (vk_left)
    {
    global.direction = 0
    hspeed +=-1
    sprite_index = spr_runL;}
   else
 if hspeed <=-8 hspeed =-8

if hspeed = 0
{
if global.direction = 1 sprite_index = spr_idleR
else sprite_index = spr_idleL

}
}

if (vsp < 10) vsp += grav;

if (place_meeting (x,y-4,obj_ground))
{
    vspeed = key_jump * -jumpspeed
    
    if vspeed<=-1
    {
    if keyboard_check (vk_right)
    {hspeed +=1
    global.direction = 1  
    sprite_index = spr_jumpupR}
    else
    if hspeed >=8 hspeed =8
    if keyboard_check (vk_left)
    {
    global.direction = 0
    hspeed +=-1
    sprite_index = spr_jumpupL;}
   else
  if hspeed <=-8 hspeed =-8
    }
    if vspeed = 0
    {
    if global.direction =1  sprite_index = spr_jumpapexR
    else sprite_index = spr_jumpapexL
    }
        if vspeed>=1
    {
    if keyboard_check (vk_right)
    {hspeed +=1
    global.direction = 1  
    sprite_index = spr_jumpdownR}
    else
    if hspeed >=8 hspeed =8
    if keyboard_check (vk_left)
    {
    global.direction = 0
    hspeed +=-1
    sprite_index = spr_jumpdownL;}
   else
 if hspeed <=-8 hspeed =-8
    }
    
    
    
}

//if (place_meeting (x+4,y,obj_ground))
//{
//    while(!place_meeting(x+4),y,obj_ground))
//{
//    x += sign(hsp);
//}
//    hsp = x;
//}

if (place_meeting (x,y+vsp,obj_ground))
{
    while(!place_meeting(x,y+sign(vsp),obj_ground))
{
    y += sign(vsp);
}
    vsp = 0;
}
y += vsp;

 

There might even be some extra code no longer being used belonging to the tutorial. Any help or support on how to even start doing this would be greatly appreciated. My undertanding on coding language is ugh.

Edited by Nirwanda

Share this post


Link to post
Share on other sites

Hey there! ^^

 

I believe there was a problem with 'if' statements, that resulted in multiple things happening at once, which could overwrite previous code, such as giving incorrect sprite in the end.

I gave it a try and modified your code. Not sure if I got it correctly, but now, when you'll say what's wrong, then I should know where to tweak things, hopefully.  :P

 

What I did:

- I've reorganized your code by moving things around.

- I've merged functionality, because for example, in your code there was horizontal movement (buttons) handled separately when on ground and when in mid-air - now it's all in one place regardless of on-ground/falling factors, since it was the same thing in the code (the same values and such)

- sprite selection is no longer handled by pressed buttons; it is now dependable on horizontal speed, vertical speed and global direction variable (more automatic; less lines of code)

- fixed: using multiple variables for handling one thing, that didn't work together - replaced with a single variable 'hsp' / 'vsp'

 

Here's the modified code:

 

 

key_jump = keyboard_check_pressed(ord("Z"));

// HORIZONTAL MOVEMENT
if (keyboard_check(vk_left) && keyboard_check(vk_right)) ||
    (!keyboard_check(vk_left) && !keyboard_check(vk_right))
    // holding both directional keys or neither of them - stop movement
{
    if hsp >= 1 hsp -= 1; // reduce speed
    else if hsp <= -1 hsp += 1; // reduce speed
    else if hsp != 0 hsp = 0; // stop
}
else if keyboard_check(vk_right) // holding RIGHT key
{   
    global.direction = 1; // set direction
    hsp = min(hsp + 1, 8); // increase speed; limit to 8
}
else if keyboard_check(vk_left) // holding LEFT key
{
    global.direction = 0; // set direction
    hsp = max(hsp - 1, -8); // increase speed; litit to -8
}

// WHEN ON GROUND
if (place_meeting(x, y+1, obj_ground))
{
    // JUMP
    if key_jump vsp = -jumpspeed // pressing jump button
    
    // SET SPRITE (STANDING/RUNNING ANIMATION)
    if hsp == 0 // when idle
    {
        if global.direction == 1 sprite_index = spr_idleR; // idle: right
        else sprite_index = spr_idleL; // idle: left
    }
    else if hsp > 0 sprite_index = spr_runR; // running: right
    else if hsp < 0 sprite_index = spr_runL; // running: left
    
}

// WHEN IN MID-AIR
else // when in mid-air
{
    // GRAVITY
    vsp = min(vsp + grav, 10) // increase falling speed; limit to 10
    
    // SET SPRITE (JUMPING ANIMATION)
    if vsp <= 0 // going UP
    {
        if global.direction == 1 sprite_index = spr_jumpupR; // up-right
        else sprite_index = spr_jumpupL; // up-left
    }
    else // falling DOWN
    {
        if global.direction = 1 sprite_index = spr_jumpdownR; // down-right
        else sprite_index = spr_jumpdownL; // down-left
    }
}

// COLLIDE WITH WALLS (HORIZONTALLY)
if (place_meeting (x+hsp, y, obj_ground))
{
    while(!place_meeting(x+sign(hsp), y, obj_ground))
    {
        x += sign(hsp);
    }
    hsp = 0;
}

// COLLIDE WITH WALLS (VERTICALLY)
if (place_meeting(x, y+vsp, obj_ground))
{
    while(!place_meeting(x, y+sign(vsp), obj_ground))
    {
        y += sign(vsp);
    }
    vsp = 0;
}

// MOVE CHARACTER
x += hsp;
y += vsp; 

 

 

 

There may be errors, because it's been a while, since I've toyed with GM last time, so I could mess up some stuff.  :giggle:

I've added comments all over the place to help you understand, what each line of code does. It also makes things more readable, so it's a good habit.

And of course, remove the `friction` variable in the create event - stopping character is handled now in the code.

 

Hope that will not explode!  :P

Edited by Rikifive
Updated Code

Share this post


Link to post
Share on other sites

Thanks for your help, Riki! Jumping and falling seem to work right (albeit kinda slowly, numbers need tweaking) but she doesn't run right or left and she falls through the ground. :(

Share this post


Link to post
Share on other sites

Lol, insanity ensues: she constantly alternates between the falling and idle animations. Running animation works, but she stays in place and jumping doesn't work.

Thanks again riki!

Share this post


Link to post
Share on other sites

After some private detailed conversation and taking a closer look at the project it seems, that the problem is related to different sprite sizes (collision boxes) between frames. Whenever character changes the animation (from idle to running, for example) then the collision box changes as well giving extremely weird results, like getting stuck in walls or that insane animation changing.

 

Organizing the sprite sheet to give the same size for each frame should fix the issue, at least partially. :P 

Share this post


Link to post
Share on other sites

You make me feel uncomfortable now. :P

Happy could help!

 

I think it can be considered as solved. ^^ If issues will arise, just say a word. :)

 

This thread is closed, due to problem being solved. If for some reason anybody would like to re-open this thread, just send a message to moderator or report the first post on this thread and ask for its re-opening in the reason box. (=

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

  • Recently Browsing   0 members

    No registered users viewing this page.

×