[GameMaker - GML] Platformer Sprite Control help please!

edited in Questions and Answers
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:

//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

  • Hmm - must have missed this yesterday!

    Did you manage to figure it out? :)

    If should be as simple as putting an else between your movement checks, i.e.:

    //Right Movement
    if (key_right) { 
           image_xscale = 1; sprite_index = sprite_running; image_speed = animation_speed; 
    }else if (key_left) { 
           image_xscale = -1; sprite_index = sprite_running; image_speed = animation_speed; 
    }else if (keyboard_check_released(vk_right)) || (keyboard_check_released(vk_left)){ 
           sprite_index = sprite_standing; image_speed = 0; 
    }


    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 :).

  • Hi there!
    @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:

    //sprite
    if xsp>0 image_xscale = 1; //xsp is the same as your hsp variable
    if xsp<0 image_xscale = -1;
    if abs(xsp)>0 && grounded 
        sprite_index=spr_p1_ltsolrun; //this checks for movement in any direction(left/right) and if the player is on the ground
    if xsp=0 and grounded //checks if a player is grounded and standing still
        {
        if !attacking && !specattacking//checks that the player is not attacking(just standing still
            sprite_index=spr_p1_ltsolstand;
        if attacking>0 //this means the player is attacking(variable is set to 1 when player attacks)
            {
            attacking-=1;
            if !specattacking//check that the player is not doing a special attack
                sprite_index=spr_p1_ltsolstandatt;
            }
        if specattacking>0//same as attacking, just checks for special attack
            {specattacking-=1;
            sprite_index=spr_p1_ltsolstandspec;
            }  
        }
    if ysp<0 sprite_index=spr_p1_ltsoljump;//(ysp is the same as your vsp)checks whether player is jumping(moving upwards)
    if ysp>0 sprite_index=spr_p1_ltsolfall;//checks if player is falling(moving downwards)
    image_speed=0.3;// this is just the value that worked best for my animation


    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
  • Thanks guys! Will have a look when I get home to see if I can get it to work :)
Sign In or Register to comment.