Legacy GM Is a crouch animation possible with this setup?

Create:

Code:
grav = 0.3;
hsp = 0;
vsp = 0;
jumpSpeed = 5;
moveSpeed = 3;
Step:


Code:
// Get input
key_Left = -keyboard_check(vk_left); key_Right = keyboard_check(vk_right); key_Jump = keyboard_check_pressed(vk_up); key_Crouch = keyboard_check_pressed(vk_down);

// Use input
move = key_Left + key_Right; hsp = move * moveSpeed; if (vsp < 10) { vsp += grav; };

if (place_meeting(x, y + 1, obj_jwall)) { vsp = key_Jump * -jumpSpeed }

// Sound
if (key_Jump) && (place_meeting(x, y + 1, obj_jwall))
{
audio_play_sound (jplayerjump, 0, false);
}
if (key_Jump) && (place_meeting(x+1,y,obj_jwall) || place_meeting(x-1,y,obj_jwall))
{
vsp = key_Jump * -jumpSpeed; audio_play_sound (jplayerjump, 0, false);
}
// H Collisions
if (place_meeting(x + hsp, y, obj_jwall)) { while (!place_meeting(x + sign(hsp), y, obj_jwall)) { x += sign(hsp); } hsp = 0; } x += hsp;

// v Collisions
if (place_meeting(x, y + vsp, obj_jwall)) { while (!place_meeting(x, y + sign(vsp), obj_jwall)) { y += sign(vsp); } vsp = 0; } y += vsp;
    
// Animations
if (!place_meeting(x,y+1,obj_jwall))
{
 sprite_index = sp_jplayerjumpalt;
 image_speed = .8;
 if (sign(vsp) > 0) image_index = 0; else sprite_index = sp_jplayerjumpalt;
}
else
{
 image_speed = 1;
 if (hsp == 0)
 {
  sprite_index = sp_jplayer;
 }
 else
 {
  sprite_index = sp_jplayerwalk;
  image_speed = .4;
 }
}
// flip
if (hsp != 0) image_xscale = sign(hsp);
 

samspade

Member
Yes, but difficult. I would recommend looking into something like a simple state machine. There are many GM tutorials about them on Youtube and this forum. If you don't switch to something like that, every new thing you do will become increasingly more complicated and more likely to break your code, be difficult to modify and so on. I think this is one of the better ones:


For example, to do a crouch in your current code, you would have to add a crouch button, and then in every single place it applies, add a conditional to check if crouching. So movement, conditional to prevent or slow it perhaps if crouching. Sound, conditional to change sound. Animation, conditional to change animation. Jump conditional to prevent or change jumping. At the moment, this isn't super difficult, but it is time consuming. And then imagine you want to add another thing, now you have to add a new conditional every time, and may even have to add sub conditionals inside of other ones.

In other words, once you get beyond one or two things, you probably want to break your code up in some way and states are one of the easiest to explain and implement for this type of thing.
 
Top