1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 Slime no longer jumps after adding in specific animations.

Discussion in 'Programming' started by AtomicBlade, Jan 22, 2020.

  1. AtomicBlade

    AtomicBlade Member

    Joined:
    Jun 25, 2019
    Posts:
    11
    I tried to make some code for a slime moving randomly. It all worked fine, until I decided to change the sprite index based off of where it was during the jump. Eg, an animation just before the jump, an animation as it falls, etc. Unfortunately, after adding in the code, the slime stopped jumping, instead hanging in the air at sprite index 7. I identified the code that caused this and removed it, only for the slime to just run through its animation like normal, despite the code, and it still refused to jump. I'd like to know how to make the slime jump again, as well as how to change the other line of code so that it works as intended.
    Code:

    vsp = vsp + grv;

    //Horizontal Collision

    if (place_meeting(x+hsp,y,oWall))
    {
    while (!place_meeting(x+sign(hsp),y,oWall))
    {
    x = x + sign(hsp);
    }

    hsp = -hsp * 0.8;

    }

    if(canmove) x = x + hsp;

    //Vertical Collision

    if (place_meeting(x,y+vsp,oWall))
    {
    while (!place_meeting(x,y+sign(vsp),oWall))
    {
    y = y + sign(vsp);
    }
    vsp = 0;

    if(alarm[0] < 1) alarm[0] = random_range(fps*0.5,fps*4);

    canmove = false;

    } else canmove = true;

    y = y + vsp;

    //Jumping Animation

    if (!jumping) {
    if(image_index > 2) image_index = 0;
    }

    if (jumping){
    if (place_meeting(x,y+vsp,oWall)) {
    if (landing = false) {
    if(image_index > 4) {
    hsp = random_range(-4,4)
    vsp = random_range(-7,-10);
    }
    }
    if(landing) {
    image_speed = 1;
    if (image_index = image_number-1) {
    landing = false;
    jumping = false;
    }
    }
    }

    if (!place_meeting(x,y+vsp,oWall)) {

    image_speed = 1;

    if (vsp < -0.5) {

    if(image_index > 6) {
    image_speed = 0;
    image_index = 7;

    }

    }

    if(vsp > -0.5) && (vsp < 0.5) image_index = 7; This is the line that caused the first issue.

    if(vsp > 0.5) {

    image_speed = 1;

    if (image_index > 10) {
    image_speed = 0;
    image_index = 10;
    landing = true;
    }
    }
    }
    }
     
  2. relic1882

    relic1882 Member

    Joined:
    Apr 5, 2018
    Posts:
    32
    Can you put this code in by inserting it as code? It's hard to read with all of the brackets lined up like that.
     
  3. relic1882

    relic1882 Member

    Joined:
    Apr 5, 2018
    Posts:
    32
    Your jumping animations are being calculated after you have already done the calculation for y. But before that calculation you have collision detection for the wall that checks below the slime for the wall and essentially sets the vsp speed back to 0, so the y calculation never changes before coming back to the jumping calculation. I believe your y += vsp needs to happen after you factor in jumping, or at least tell the slime it's jumping before having it detect a wall collision underneath it and resetting vsp to 0.
     
  4. AtomicBlade

    AtomicBlade Member

    Joined:
    Jun 25, 2019
    Posts:
    11
    That makes sense. I’ll see if that works. Also, I’m new to this forum, how do you insert it as code?
     
  5. AtomicBlade

    AtomicBlade Member

    Joined:
    Jun 25, 2019
    Posts:
    11
    Hey, thanks for your help. The slime now jumps as intended, but will not run the animation of landing when it lands. Any ide on how to fix that? Here's the new code.
    Code:
    vsp = vsp + grv;
    
    
    
    if (vsp > 0) falling = true;
    
    //Jumping Animation
    
    if (!jumping) && (!landing) && (place_meeting(x,y+vsp,oWall)) {
        if(image_index > 2) image_index = 0;
    }
    if (jumping) && (!landing) {
        if (place_meeting(x,y+vsp,oWall)) {
            if (landing = false) {
                if(image_index > 4) {
                    alarm[1] = 5;
                    //hsp = random_range(-4,4);
                    //vsp = random_range(-7,-10);
                    //jumping = false;
                }
            }
        }
    }
    
    if(place_meeting(x,y+vsp,oWall)) {
            if(landing) {
                image_speed = 1;
                if (image_index = image_number-1) {
                    landing = false;
                    jumping = false;
                    falling = false;
                }
            }
    
    }
          
        if (!place_meeting(x,y+vsp,oWall)) {
            image_speed = 1;
            if (vsp < -0.5) {
                if(image_index > 6) {
                    image_speed = 0;
                    image_index = 7;
                  
                }
            }
            if(vsp = 0) {
                image_index = 7;
                landing = true;
            }
            if (landing) {
                image_speed = 1;
                if (image_index > 10) {
                    image_speed = 0;
                    image_index = 10;
                    landing = true;
                }
              
            }
        }
      
    
    
    //Horizontal Collision
    if (place_meeting(x+hsp,y,oWall))
    {
        while (!place_meeting(x+sign(hsp),y,oWall))
        {
            x = x + sign(hsp);
        }
        hsp = -hsp * 0.8;
    }
    if(canmove) x = x + hsp;
    
    //Vertical Collision
    if (place_meeting(x,y+vsp,oWall))
    {
        while (!place_meeting(x,y+sign(vsp),oWall))
        {
            y = y + sign(vsp);
        }
        vsp = 0;
        if(alarm[0] < 1) && (!place_meeting(x,y+sign(vsp),oWall)) alarm[0] = random_range(fps*0.5,fps*4);
        canmove = false;
      
    } else canmove = true;
    
    y = y + vsp;
    
    
    Alarm[0]:
    jumping = true;

    Alarm[1]:
    hsp = random_range(-4,4);
    vsp = random_range(-7,-10);
    jumping = false;
     
  6. relic1882

    relic1882 Member

    Joined:
    Apr 5, 2018
    Posts:
    32
    Code:
    if (!place_meeting(x,y+vsp,oWall)) {
            image_speed = 1;
            if (vsp < -0.5) {
                if(image_index > 6) {
                    image_speed = 0;
                    image_index = 7;
                  
                }
            }
            if(vsp = 0) {
                image_index = 7;
                landing = true;
            }
            if (landing) {
                image_speed = 1;
                if (image_index > 10) {
                    image_speed = 0;
                    image_index = 10;
                    landing = true;
                }
              
            }
        }
    Maybe add a (!landing) check to this line

    if(vsp = 0) && (!landing)

    because when you check it now, it sets the image index to 7 every time the code runs again so the next if (landing) doesn't go far sure to that reset every time.

    Also is there a reason you have landing = true within your landing check code if the condition was already true when it started?
     
  7. AtomicBlade

    AtomicBlade Member

    Joined:
    Jun 25, 2019
    Posts:
    11
    The reason is because I’m dumb as a rock. The slime works fine now, except from it not playing the landing animation as it lands.
     
  8. AtomicBlade

    AtomicBlade Member

    Joined:
    Jun 25, 2019
    Posts:
    11
    Never mind, I’ve figured it out. You’ve been really helpful, can you help me with another unrelated problem to do with throwable grenades?
     
  9. relic1882

    relic1882 Member

    Joined:
    Apr 5, 2018
    Posts:
    32
    What are you having trouble with?
     
  10. AtomicBlade

    AtomicBlade Member

    Joined:
    Jun 25, 2019
    Posts:
    11
    Don’t worry about it. The grenades would teleport and bounce in unnatural ways, but I looked up a bouncy ball tutorial and figured that out. If you ever need grenade code, I can provide it.
    Thanks a lot for replying. You’ve been really helpful.
     

Share This Page