[GameMaker - GML] Platformer Sprite Control help please!
Hi guys,
I've gone around the interwebs trying to get a nice platformer script. i eventually found on off of Youtube but the movements were only for a block, so no sprite animations. I tried my best adjusting and tweaking as best I knew how and below is what I've been able to do, also my 1st go at GML and Gamemaker.
Please help me iron out those pesky sprite glitches when pressing 2 or more keys at the same time. Feel free to play the game maker project at the bottom to get a feel for it.
I have 2 scripts...
1) Player Setup:
2) Movement & Sprite Script:
Help much appreciated! Thank you!
I've gone around the interwebs trying to get a nice platformer script. i eventually found on off of Youtube but the movements were only for a block, so no sprite animations. I tried my best adjusting and tweaking as best I knew how and below is what I've been able to do, also my 1st go at GML and Gamemaker.
Please help me iron out those pesky sprite glitches when pressing 2 or more keys at the same time. Feel free to play the game maker project at the bottom to get a feel for it.
I have 2 scripts...
1) Player Setup:
//Player Setup hsp = 0; //Horizontal Move Speed Container vsp = 0; //Verticle Move Speed Container grav = 0.45; //Gravity jumpspeed = 7; //Jump Speed movespeed = 3; //Movement Speed animation_speed = 1.2; //Sprite Animation Speed sprite_running = spr_player_running_axe; //Running Sprite sprite_standing = spr_player_stand_axe; //Standing Sprite sprite_jumping = spr_player_jump_axe // Jumping Sprite
2) Movement & Sprite Script:
// --- KEYBOARD INPUT'S --- // key_right = keyboard_check(vk_right); key_left = keyboard_check(vk_left); key_jump = keyboard_check_pressed(vk_up); key_up = keyboard_check(vk_up); // --- CHARACTER CONTROL --- // //Left & Right move = -key_left + key_right; hsp = move * movespeed; jumpState = false; //Jump if (vsp < 10) { vsp += grav; jumpState=true; } //React to gravity if (place_meeting(x,y+1,obj_wall)) { vsp = key_jump * -jumpspeed; jumpState=false;} // --- SPRITE CONTROL --- // //Right Movement if (key_right) { image_xscale = 1; sprite_index = sprite_running; image_speed = animation_speed; } //Left Movement if (key_left) { image_xscale = -1; sprite_index = sprite_running; image_speed = animation_speed; } //No Movement if (keyboard_check_released(vk_right)) || (keyboard_check_released(vk_left)){ sprite_index = sprite_standing; image_speed = 0; } //No Moonwalking if (key_right) && (key_left) { sprite_index = sprite_standing; image_speed = 0; } //Jump Movement if (keyboard_check(vk_up) || (keyboard_check(vk_up))&&(key_right)) { sprite_index = sprite_jumping; } if (keyboard_check_released(vk_up)){ sprite_index = sprite_standing; image_speed = 0; } // --- COLLISION DETECTION --- // //Horizontal Collision if (place_meeting(x+hsp,y,obj_wall)) { while(!place_meeting(x+sign(hsp),y,obj_wall)) { x += sign(hsp); } hsp = 0; sprite_index = sprite_standing; image_speed = 0; /* Wall mount if (jumpState) { if (sign(vsp)=1) { vsp -= 0.5; show_debug_message(vsp); //vsp = 0; } else { vsp += 0.5; } } */ } x += hsp; //Vertical Collision if (place_meeting(x,y+vsp,obj_wall)) { while(!place_meeting(x,y+sign(vsp),obj_wall)) { y += sign(vsp); } vsp = 0; } y += vsp;
Help much appreciated! Thank you!
zip

zip

big_heart_platformer.gmx.zip
1M
Comments
Did you manage to figure it out? :)
If should be as simple as putting an else between your movement checks, i.e.:
That way it only ever executes one of the buttons being pressed. If that doesn't work let me know and I'll open up the project :).
@Bensonance 's code looks good! I think the only thing is that the sprite will then change direction as soon as the different direction is pressed(if i'm not mistaken?).
It's not an issue but it is just nice to keep in mind. :)
Here's the code I wrote for my game:
I hope this helps. It may be way more complicated than it needs to be but it's what I ended up with after fine tuning and a lot of trial and error haha. :)
Have an awesome evening!
Karl