1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GM:S 1.4 SOLVED: Player movement sprites

Discussion in 'Programming' started by Axl Trauts, Jun 26, 2016.

  1. Axl Trauts

    Axl Trauts Member

    Joined:
    Jun 26, 2016
    Posts:
    28
    Hi,

    I know the basics of how to move a player. In my case it's a plane in a top view shoot-em up game such as 1943, and I know how to change the sprite when it moves to the left, stops at a frame while the left key is pressed, and when I stop pressing it, the sprite changes to the starting sprite.

    Let me explaing better:
    I have two images for the Left animation: the first is a small turning to the left and the second is a full turn. If I keep Left pressed, the image stays in the full turn and that is perfect.
    If I stop pressing Left, the sprite changes to the original starting sprite, witth the plane just going forward. That's fine too.

    What I need is to add a reverse animation when you release the Left key, so the left sprite goes from the full turn left to the small turn, then stop the animation AND THEN change the animation to the starting sprite. I can get keyboard_check_release to work and keyboard_check(vk_nokey) to work, but not both on the same code. Maked sense now that I think of it.

    this is the code. How can I make it work?

    if keyboard_check(vk_left)
    {
    if x>40
    { x=x-6; }
    sprite_index=spr_player_izq; image_speed=0.02;
    if image_index>1 {image_speed=0}
    }
    if keyboard_check_released(vk_left)
    {
    image_speed=-0.02;
    //Not sure about this one if image_index>1 {image_speed=0}
    }
    if keyboard_check(vk_nokey)
    {
    sprite_index=spr_player_start;
    }
     
  2. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    Hi and welcome to the GMC!

    The "Not sure about" part should be left away.
    The if keyboard_check(vk_nokey) { ... } is also a bad idea, so leave that away as well.

    Instead, use this code in the animation end event:
    if sprite_index == spr_player_izq && image_speed < 0 {
    image_index = 0;
    image_speed = 0; // or whatever this is supposed to be
    sprite_index = spr_player_start;
    }
     
  3. Axl Trauts

    Axl Trauts Member

    Joined:
    Jun 26, 2016
    Posts:
    28
    That´s glorious! I wasn´t aware of Animation End. I'll keep animating the rest of the movements. My thanks!
     
  4. Axl Trauts

    Axl Trauts Member

    Joined:
    Jun 26, 2016
    Posts:
    28
    Oh, by the way, I made it work fine. When I move left and stop, it reverses and goes to the starting sprite. But this is maybe a minor issue, but if just after releasing left I press right, the animation goes super fast from the last image on the left sprite to the last image on the right sprite, same applies if I move from right to left. It´s not as the same speed as if I release left, wait a bit and then press right or viceversa. Maybe because image_speed adds the value, I don't know.
     
  5. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    Make it so that this piece of code:
    sprite_index=spr_player_izq; image_speed=0.02;
    if image_index>1 {image_speed=0}

    only executes if the sprite is not facing right.

    That should fix the problem for when turning from right to left.
     
  6. Axl Trauts

    Axl Trauts Member

    Joined:
    Jun 26, 2016
    Posts:
    28
    that worked like a charm, thank you very much! I´ll keep working on the other diagonal directions.

    I case somebody wants to know (I change the sprite and object names to fit my game better):

    In Step Event:
    //Mover a la izquierda - Move Left
    if keyboard_check(vk_left)
    {
    if x>40
    { x=x-6; }
    if sprite_index!=spr_player_2_der
    { sprite_index=spr_player_2_izq; image_speed=0.1;
    if image_index>1 {image_speed=0}
    }
    }
    //Mover a la derecha - Move Right
    else if keyboard_check(vk_right)
    {
    if x>room_width -40
    { x=x-6; }
    if sprite_index!=spr_player_2_izq
    { sprite_index=spr_player_2_der; image_speed=0.1;
    if image_index>1 {image_speed=0}
    }
    }

    //soltar la izquierda - Release Left
    if keyboard_check_released(vk_left)
    { image_speed=-0.1; }
    //soltar la derecha - Release Right
    else if keyboard_check_released(vk_right)
    { image_speed=-0.1; }
    In Animation End Event:
    if sprite_index == spr_player_2_izq && image_speed < 0
    {
    image_index = 0;
    image_speed = 0.1; // velocidad inicial
    sprite_index = spr_player_2;
    }
    else if sprite_index == spr_player_2_der && image_speed < 0
    {
    image_index = 0;
    image_speed = 0.1; // velocidad inicial
    sprite_index = spr_player_2;
    }​
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice