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

Slip and Slide, Kirby!

M

MegaGamer99

Guest
Greetings, Game Makers!

I've been working on my engine, and when I was about to add a sliding action, he doesn't move!
It was probably because of one of the pieces of code on his ducking action, since I couldn't get him to stop moving when he's ducking. I cleared out the sliding action, so I can start over.

So to recap, I need help on giving Kirby the ability to slide, and here's how it works:
  • While Ducking, pressing the Jump button while on the ground should cause Kirby to slide.
  • While Sliding, his initial hspeed should be like 8, and his hspeed should go down by 1.
  • He should exit Sliding mode when he either falls off of the ground, his hspeed hits 0, or when he hits the wall.
I want the code to be short and simple as possible, and if the ducking code I mentioned earlier is the road block, then we should at least try to find an alternative solution to stop the player's hspeed dead in his tracks when Ducking. I will appreciate any help given.

Here is the code for reference:

Code:
Create Event:

execute code:

//-----Intitialize Variables-----//
movespeed = 2; //Moving speed (2 pixels per frame)
flyspeed = -4;
jumpspeed = -7; //Jumping speed (negative because it goes up)
grav = 0.6; //Gravity strength (usually a small value)
//These variables are actually used to move the player
hsp = 0; //Horizontal speed
vsp = 0; //Vertical speed
ground = 1; //Whether the player is on the ground
flying = false;
falling = false;
step = 0;
ducking = false;


Step Event:

execute code:

//-----Keyboard Input-----//
Key_Left = keyboard_check(vk_left);
Key_Right = keyboard_check(vk_right);
Key_Jump = keyboard_check_pressed(ord('Z'));
//This one's for variable jump height:
Key_Fall = keyboard_check_released(ord('Z'));
Key_Attack = keyboard_check(ord('X'));
Key_Down = keyboard_check(vk_down);

//Horizontal Motion
if (Key_Left) and (!ducking)
{
    hsp = -movespeed; //Walk left
    image_xscale = -1; //Face left
}
if (Key_Right) and (!ducking)
{
    hsp = movespeed; //Walk right
    image_xscale = 1; //Face right
}

//Movement Key Release
if (!Key_Left) and (!Key_Right) hsp = 0;

//Vertical Motion
//Is the player in the air?
if (place_meeting(x,y+1,obj_blockpar)) ground = 1;
else ground = 0;

if (Key_Jump and ground and !ducking)
{
   vsp = jumpspeed;
}
//Variable Jumping
if (!flying)
{
if (Key_Fall and !ground and vsp < -3) vsp = -2;
}

//Fall with gravity
if (!ground) vsp += grav;
//Limit Vspeed
if (!flying)
{
    if (vsp > 6) vsp = 6;
}

if (flying)
{
    if (vsp > 1) vsp = 1;
}

//Flying
if (!ground) and (Key_Jump)
flying = true;

if (Key_Jump) and (flying)
vsp = flyspeed;

if (Key_Attack and flying)
{
    flying = false;
}

if (flying) faling = false;

//Falling
if (!ground and vsp > 0 and !falling and !flying)
{
    step += 1;
    }else{
    step = 0;
}

if (step = 17 and !falling and !flying)
{
    if (step < 17) step = 17;
    falling = true;
}

if (ground and falling)
{
    vsp = -4;
}

if (falling) and (!vsp)
falling = false;

//Ducking
if (Key_Down and ground and !ducking and !flying and !falling and vsp = 0)
ducking = true;

if (ducking) and (sprite_index = spr_player)
sprite_index = spr_player_duck;
if (!ducking) and (sprite_index = spr_player_duck)
sprite_index = spr_player;

if (!Key_Down) and (ducking)
ducking = false;

if (ducking) and (Key_Left) and (hsp < 0) hsp = 0;
if (ducking) and (Key_Right) and (hsp > 0) hsp = 0;

//Sliding


//Horizontal Collision
if (place_meeting(x+hsp,y,obj_blockpar))
{
    while(!place_meeting(x+sign(hsp),y,obj_blockpar))
    {
        x += sign(hsp);
    }
    hsp = 0;
}
x += hsp;

//Vertical Collision
if (place_meeting(x,y+vsp,obj_blockpar))
{
    while(!place_meeting(x,y+sign(vsp),obj_blockpar))
    {
        y += sign(vsp);
    }
    vsp = 0;
}
y += vsp;
 
Last edited by a moderator:

jo-thijs

Member
I would just have a variable sliding, which is set to true if kirby is ducking and trying to jump.
When setting it to true, I would assign kirby his initial speed.

Then, whenever one of the conditions is met, you reset sliding to false.

For as long as sliding is true, you ignore normal input or friction and just keep subtracting 1 from hspeed,
setting the animation to a sliding animation and applying movement and collision.
 
M

MegaGamer99

Guest
if (ducking) and (Key_Jump) and (!sliding) sliding = true;
if (sliding) and (ground)
{
if (image_index = -1) hsp = -8;
if (image_index = 1) hsp = 8;
}

Here's the code for my sliding action so far. When I tested the game, he wasn't doing anything when I pressed the two buttons. I still believe it's because of this part of the ducking code:

if (ducking) and (Key_Left) and (hsp < 0) hsp = 0;
if (ducking) and (Key_Right) and (hsp > 0) hsp = 0;

The reason I made that piece is because while coding for Kirby to duck, he wouldn't stop while I was pressing the horizontal keys. So I might need some assistance for some modifications to the ducking code. Oh, and like I said earlier, look for my Player's code above for reference.
 

jo-thijs

Member
I don't see why you use image_index = -1 and image_index = 1.
I suppose that is supposed to be hsp = 8 * image_xscale?

Anyway, those 2 lines:
Code:
if (ducking) and (Key_Left) and (hsp < 0) hsp = 0;
if (ducking) and (Key_Right) and (hsp > 0) hsp = 0;
would indeed cease movement, but you can just add an extra check to see if sliding is false to solve that problem.
 

NicoDT

Member
Hi!
You should look into finite state machines (there are some good videos on that, it will help you organize better your code).

Regarding your code

Code:
if (ducking) and (Key_Jump) and (!sliding) sliding = true;
if (sliding) and (ground)
{
if (image_index = -1) hsp = -8;             <------- I believe you meant to write image_xscale, instead of image_index
if (image_index = 1) hsp = 8;
}
If it's ducking, it shouldn't be sliding. So you could have a variable called "state", which could have one of the following values: "sliding" / "ducking" / "jumping", etc.

Then the code can be:

Code:
if (state = "ducking") {
     if (key_jump) {
                 state = "sliding"
                 hsp = 8   * image_xscale
     }
}

if (state = "sliding") {
    if (hsp <= 0) state = "ducking"; // or standing, or whatever you want
        else hsp -= 1 ;
    if (!ground) state = "falling" // or jumping, or whatever you want
    if  place_meeting ( x+ hsp , y, obj_wall)     state = "ducking"
}
 
M

MegaGamer99

Guest
Anyway, those 2 lines:
Code:
if (ducking) and (Key_Left) and (hsp < 0) hsp = 0;
if (ducking) and (Key_Right) and (hsp > 0) hsp = 0;
would indeed cease movement, but you can just add an extra check to see if sliding is false to solve that problem.
*facepalms* Why didn't I think of that?
 
M

MegaGamer99

Guest
Hey, I got kirby to move when put a check on my duck code! Now all I gotta do is make him slow down and put him back to normal mode. Be back in a jiffy!
 
M

MegaGamer99

Guest
if (ducking) and (Key_Jump) and (!sliding) sliding = true;
if (sliding) and (ground)
{
if (image_xscale = -1) hsp = -8;
if (image_xscale = 1) hsp = 8;
}
if (sliding) and (hsp > 0) hsp -= 1;
if (sliding) and (hsp < 0) hsp += 1;

At the bottom of this code, I implemented this piece to slow down his speed, but when I tested it, kirby's still going like he's out of control! Please help on some controlling.
 

jo-thijs

Member
That's because this part:
Code:
if (sliding) and (ground)
{
if (image_xscale = -1) hsp = -8;
if (image_xscale = 1) hsp = 8;
}
will keep resetting the speed to 8.

That's why you only set the initial speed when sliding changes to true.
 
M

MegaGamer99

Guest
Oh yeah, sorry about that....

Ok I created a slidespeed variable and replaced 8 with that variable, but he isn't slowing down. What else am I missing?
 

jo-thijs

Member
Simple variable substitution won't change anything, you should rather do this:
Code:
if ducking && Key_Jump && !sliding {
    sliding = true;
    hspd = sign(image_xscale) * 8;
}
if sliding
    hsp -= sign(hsp);
 
M

MegaGamer99

Guest
That code didn't work, when i switched my code with that code, he didn't moved when sliding...
 
M

MegaGamer99

Guest
Ok now I added your code, but either direction I'm in has Kirby slowing moving to the left and when pressing one of the two movement keys, he speeds up to the wall.
 

jo-thijs

Member
I said to ignore normal input and friction when sliding is true.
You allow kirby to stop ducking while sliding and you allow him to walk normally when not ducking, aren't you?

What would cause the speed to slowly increase, I have no idea.
 
M

MegaGamer99

Guest
I'm supposed to ignore friction and input by putting checks on everything including ducking? I need a better understanding.
 

jo-thijs

Member
This part:
Code:
if (Key_Left) and (!ducking)
{
    hsp = -movespeed; //Walk left
    image_xscale = -1; //Face left
}
Needs to check if kirby isn't sliding.
That or you make sure Kirby is always ducking when he's sliding.
 
M

MegaGamer99

Guest
Got that sliding check for movement. And that the ducking is part of conditions that activate sliding. But the code still isn't working, it still moves Kirby to the left.
 
M

MegaGamer99

Guest
Sure Thing, but as a little disclaimer, I've made a few changes to the code to fit in with my code, but still uses the same behavior like you originally intended, so don't quickly jump:

if (ducking) and (Key_Jump) and (!sliding)
{
sliding = true;
hsp = sign(image_xscale) * slidespeed;
}
if (sliding)
hsp -= sign(1);

And if you want my entire code, look up at my first post for reference.
 

jo-thijs

Member
The sign(1) has to be sign(hsp).
sign(x) will return -1 if x < 0, 1 if x > 0 and 0 if x = 0.
sign(1) will return 1 and will keep pushing Kirby to the left.
 
M

MegaGamer99

Guest
You're right, I thought that part served as a template. Anyway, when I tested it out, he only skipped a few pixels and stopped dead in his tracks.
 

jo-thijs

Member
Do you've still got this part?
Code:
//Movement Key Release
if (!Key_Left) and (!Key_Right) hsp = 0;
Because that might not execute if Kirby is sliding.
 
M

MegaGamer99

Guest
Thanks for pointing that out. Now, what if I want to slow down the friction?
 

jo-thijs

Member
Replace:
Code:
hsp -= sign(hsp);
with:
Code:
if abs(hsp) <= sliding_friction
    sliding_friction = 0;
else
    hsp -= sign(hsp) * sliding_friction;
 
M

MegaGamer99

Guest
Ok, so do I have to create a create variable for sliding_friction?
 
M

MegaGamer99

Guest
Ok dropped the code and the variable, but when he slowed down after sliding, he's not stopping, he's just moving very slow.
 

jo-thijs

Member
My bad, made a typo again.
It's really late here, 3:40 AM.

This:
Code:
    sliding_friction = 0;
should have been:
Code:
    hsp = 0;
 
M

MegaGamer99

Guest
Thanks for your help, now my sliding action is fully functional! All I gotta do now is --

Wait, Just a minute... the left side of my sliding action isn't working right. It only skips a few pixels and stop. Can you help fix it?
 
M

MegaGamer99

Guest
if (ducking) and (Key_Jump) and (!sliding) and (ground)
{
sliding = true;
hsp = sign(image_xscale) * slidespeed;
}
if (sliding)
{
if abs(hsp) <= sliding_friction
hsp = 0;
else
hsp -= sign(hsp) * sliding_friction;

}

if (sliding) and (!hsp)
sliding = false;

I believe that it's only doing the smooth slide for the right instead of left. Is there a way you could fix that?

BTW, If you want my player's code, take a look:

Code:
Create Event:

execute code:

//-----Intitialize Variables-----//
movespeed = 2; //Moving speed (2 pixels per frame)
flyspeed = -4;
slidespeed = 6;
sliding_friction = 0.5;
jumpspeed = -7; //Jumping speed (negative because it goes up)
grav = 0.6; //Gravity strength (usually a small value)
//These variables are actually used to move the player
hsp = 0; //Horizontal speed
vsp = 0; //Vertical speed
ground = 1; //Whether the player is on the ground
flying = false;
falling = false;
step = 0;
ducking = false;
sliding = false;


Step Event:

execute code:

//-----Keyboard Input-----//
Key_Left = keyboard_check(vk_left);
Key_Right = keyboard_check(vk_right);
Key_Jump = keyboard_check_pressed(ord('Z'));
//This one's for variable jump height:
Key_Fall = keyboard_check_released(ord('Z'));
Key_Attack = keyboard_check(ord('X'));
Key_Down = keyboard_check(vk_down);

//Horizontal Motion
if (Key_Left) and (!ducking) and (!sliding)
{
    hsp = -movespeed; //Walk left
    image_xscale = -1; //Face left
}
if (Key_Right) and (!ducking) and (!sliding)
{
    hsp = movespeed; //Walk right
    image_xscale = 1; //Face right
}

//Movement Key Release
if (!Key_Left) and (!Key_Right) and (!sliding) hsp = 0;

//Vertical Motion
//Is the player in the air?
if (place_meeting(x,y+1,obj_blockpar)) ground = 1;
else ground = 0;

if (Key_Jump and ground and !ducking and !sliding)
{
   vsp = jumpspeed;
}
//Variable Jumping
if (!flying)
{
if (Key_Fall and !ground and vsp < -3) vsp = -2;
}

//Fall with gravity
if (!ground) vsp += grav;
//Limit Vspeed
if (!flying)
{
    if (vsp > 6) vsp = 6;
}

if (flying)
{
    if (vsp > 1) vsp = 1;
}

//Flying
if (!ground) and (Key_Jump) and (!ducking)
flying = true;

if (Key_Jump) and (flying)
vsp = flyspeed;

if (Key_Attack and flying)
{
    flying = false;
}

if (flying) faling = false;

//Falling
if (!ground and vsp > 0 and !falling and !flying)
{
    step += 1;
    }else{
    step = 0;
}

if (step = 17 and !falling and !flying)
{
    if (step < 17) step = 17;
    falling = true;
}

if (ground and falling)
{
    vsp = -4;
}

if (falling) and (!vsp)
falling = false;

//Ducking
if (Key_Down and ground and !ducking and !flying and !falling and vsp = 0)
ducking = true;

if (ducking) and (sprite_index = spr_player)
sprite_index = spr_player_duck;
if (!ducking) and (sprite_index = spr_player_duck)
sprite_index = spr_player;

if (!Key_Down) and (ducking)
ducking = false;

if (ducking) and (!ground)
ducking = false;

if (ducking) and (!sliding) and (Key_Left) and (hsp < 0) hsp = 0;
if (ducking) and (!sliding) and (Key_Right) and (hsp > 0) hsp = 0;

//Sliding
if (ducking) and (Key_Jump) and (!sliding) and (ground)
{
    sliding = true;
    hsp = sign(image_xscale) * slidespeed;
}
if (sliding)
{
if abs(hsp) <= sliding_friction
    hsp = 0;
else
    hsp -= sign(hsp) * sliding_friction;
}
if (sliding) and (!hsp)
sliding = false;

//Horizontal Collision
if (place_meeting(x+hsp,y,obj_blockpar))
{
    while(!place_meeting(x+sign(hsp),y,obj_blockpar))
    {
        x += sign(hsp);
    }
    hsp = 0;
}
x += hsp;

//Vertical Collision
if (place_meeting(x,y+vsp,obj_blockpar))
{
    while(!place_meeting(x,y+sign(vsp),obj_blockpar))
    {
        y += sign(vsp);
    }
    vsp = 0;
}
y += vsp;
 
Last edited by a moderator:
M

MegaGamer99

Guest
In the highlighted piece I've shown above, the abs sign and else hsp sign had < and -, and when I tested the game, the sliding went smoothly for the right side but on the left it only skips a few pixels and stops. Is there any way you could add that code for the left to make it more smooth for that other side?
 

jo-thijs

Member
This part is causing trouble:
Code:
if (sliding) and (!hsp)
sliding = false;
hsp is false if it is <= 0, you should change it to:
Code:
if hsp == 0
    sliding = false;
 
M

MegaGamer99

Guest
Oh yeah, thanks for pointing that out. I'm still a little new to GML BTW. Oh and one more thing, whenever I'm using decimals to move, my player's sprite gets his edge chopped off and pasted to his opposite side. Is there a way to fix that?
 

jo-thijs

Member
In the draw event, you can use this to draw Kirby:
Code:
draw_sprite_ext(sprite_index, -1, round(x), round(y), image_xscale, image_yscale, image_angle, image_blend, image_alpha);
 
M

MegaGamer99

Guest
Thanks for your time and everything! Now I'm all patched up and ready for more coding. Thanks so much!:)
 
Top