1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GML Jump Up Sprite used as standard Sprite?

Discussion in 'Programming' started by Ursidae, Dec 4, 2019.

  1. Ursidae

    Ursidae Member

    Joined:
    Dec 4, 2019
    Posts:
    5
    Hi,
    i'm pretty new to GMS2 and followed a Tutorial on Youtube. Now i'm stuck at changing sprites depending on what the character does. The Tutorial is about 2 Years old and i think they changed how things internally work...
    Heres the code:

    CREATE
    Code:
    hsp = 0;
    vsp = 0;
    grv = 0.1;
    walkspd = 4;
    STEP
    Code:
     //Get Player Input
    key_left = keyboard_check (vk_left);
    key_right = keyboard_check (vk_right);
    key_jump = keyboard_check_pressed (vk_space);
    
    //Calculate Movement
    var move = key_right - key_left;
    
    hsp = move * walkspd;
    
    vsp = vsp + grv;
    
    if (place_meeting (x,y+1,oWall)) && (key_jump)
    {
        vsp = -7;
    }
    
    
    
    //Horizontal Collison
    if (place_meeting(x+hsp,y,oWall))
    {
        while (!place_meeting(x+sign(hsp),y,oWall))
        {
        x = x + sign(hsp);
        }
        hsp = 0;
    }
    
    x = x + hsp;
    
    //Vertical Collison
    if (place_meeting(x,y+vsp,oWall))
    {
        while (!place_meeting(x,y+sign(vsp),oWall))
        {
        y = y + sign(vsp);
        }
        vsp = 0;
    }
    y = y + vsp;
    
    //Animation
    if(!place_meeting(x,y-1,oWall))
    {
        sprite_index = sPlayerA;
        image_speed = 0;
        if (sign(vsp) > 0) image_index = 1; else image_index = 0;
        
    }
    else
    {
        image_speed = 1;
        if (hsp == 0)
        {
            sprite_index = sPlayer;
        }
        else
        {
            sprite_index = sPlayerR;
        }
    }
    Hopefully someone can help =)

    Thanks
     
  2. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,119
    That looks about right. If the animation is the only thing that doesn't work then the problem might be elsewhere. Have you confirmed that the sprites are correct? What does your draw event look like? Is there any draw code that could be changing or ignoring what you do here?

    Also, when you say it doesn't work, how does it not work, are the sprites not changing at all or are they changing in different than expected ways?
     
  3. Ursidae

    Ursidae Member

    Joined:
    Dec 4, 2019
    Posts:
    5
    The character always looks up. It just change the sprite if falling down. What do you mean with draw code?

    EDIT: Just read what a draw event is. No i don't got any Draw event in the project.
     
    Last edited: Dec 4, 2019
  4. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,119
    I'm not sure what "The character always looks up. It just change the sprite if falling down" means. Reading your code that sounds like what is supposed to happen, when jumping the character should look up unless falling down. To put it another way, it looks like you should have four different 'animations':
    • idle animation - has multiple frames
    • run right animation - has multiple frames
    • jump up - single frame
    • fall down - single frame
    Which of these work do not work and in what circumstances?
     
  5. Ursidae

    Ursidae Member

    Joined:
    Dec 4, 2019
    Posts:
    5
    The only Animation that work is jump up and falling down. If i land on ground the character switches to the jump up frame..

    I have an idle frame without any animation.
    Run right has an animation
    Jump up and down is in an single sprite with 2 Frames
     
  6. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,119
    Change this:

    Code:
    
    //Animation
    if(!place_meeting(x,y-1,oWall))
    
    
    To this:

    Code:
    
    //Animation
    if(!place_meeting(x,y+1,oWall))
    
    
     
  7. Ursidae

    Ursidae Member

    Joined:
    Dec 4, 2019
    Posts:
    5
    So easy... Thank you so much for the quick answer =D

    Now the character moves as expected.
     
  8. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,119
    Great. The reason it wasn't working was because you were checking for a collision above the player. Y increases going down and decreases going up. So checking for y-1 looks for a collision one pixel above the player. Notice how your jump code uses y + 1 to check for a collision below the player.
     
  9. Ursidae

    Ursidae Member

    Joined:
    Dec 4, 2019
    Posts:
    5
    Thank you for the detailed explanation!
     

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