GML Jump animation won't work

P

PizzaGuy676

Guest
I'm making Super Mario Bros. in GameMaker, and I've run into a problem. Well, I've actually had this problem for quite a while, but it was really bothering me lately and I wanted to find a solution. Basically, I have separate sprites for Mario's walk animation and his jump. Mario does have a standing still sprite, but until recently it was part of the walk animation, and I would just refer to that frame in the animation when Mario wasn't moving. However, it looked out of place when Mario was walking, so I decided to make the standing sprite its own separate sprite. However, this led to a new problem: Mario's jump sprite wouldn't show when he jumped, it would just show him jumping in whatever sprite he was in before he jumped. So if he was standing, he would just jump while standing still, and if he was walking, he would jump while in the middle of his walking animation. Obviously, I don't want this to happen. Here is my code (in the step event for the obj_mario):

Code:
scr_mario_controls();

//Colliding with ground
if (place_meeting(x, y + 1, par_block)) {
    vspd = 0;

    
    //Jumping
    if (jump) {
        vspd = -jump_spd;
        sprite_index = spr_mario_jump;
    }
} else {
    //Gravity
    if (vspd < 10) {
        vspd += grav;
        sprite_index = spr_mario_jump;
    }
    
    if (keyboard_check_released(vk_space) and vspd < -4) {
        vspd = -4;
        sprite_index = spr_mario_jump;
    }
}

//If not moving
if ((!move_right and !move_left) || (move_right and move_left)) {
     hspd = max(abs(hspd) - frcn, 0) * sign(hspd);
     sprite_index = spr_mario_idle;
} else {
     sprite_index =! spr_mario_idle;
}


//Moving right
if (move_right) {
    if (hspd < spd) {
        hspd += frcn;
    } else {
        hspd = spd;
    }
    facing = 1;
    sprite_index = spr_mario_walk;
}

//Moving left
if (move_left) {
    if (hspd > -spd) {
        hspd -= frcn;
    } else {
        hspd = -spd;
    }
    facing = -1;
    sprite_index = spr_mario_walk;
}

//Horizontal collisions
if (place_meeting(x+hspd, y, par_block)) {
    while (!place_meeting(x+sign(hspd), y, par_block)) {
        x += sign(hspd);
    }
    hspd = 0;
}

//Move horizontally
x += hspd;

//Vertical collisions
if (place_meeting(x, y + vspd, par_block)) {
    while (!place_meeting(x, y + sign(vspd), par_block)) {
        y += sign(vspd);
    }
    vspd = 0;
}

//Move vertically
y += vspd;

//Mario turns
if ((hspd < 0 and move_right) || (hspd > 0 and move_left)) and place_meeting(x, y-1, par_block)
{
    sprite_index = spr_mario_turn;
}
Despite referring to the jump sprite multiple times in my code, the game just won't show it. There's something in there that's keeping the jump sprite from showing up, but no matter how hard I look I just can't find it. See if you can find a problem with this code.
 

obscene

Member
Your sprite is being set to jump, but then being set back to something else when it gets to those lines in the code that determine if you are moving left, right etc.

The thing to do IMO is separate your collision code from your animations and start from scratch, prioritizing them in a way that gives precedence to jumping over everything else. Use the collision code to set variables to determine what the player is doing (running, jumping, etc) and then do something with it later...

Code:
if (jump)
  {
  sprite_index==etc
  }
else if (run)
 {
  etc
 }
else if (walk)
 {
 etc
 }
 
P

PizzaGuy676

Guest
Your sprite is being set to jump, but then being set back to something else when it gets to those lines in the code that determine if you are moving left, right etc.

The thing to do IMO is separate your collision code from your animations and start from scratch, prioritizing them in a way that gives precedence to jumping over everything else. Use the collision code to set variables to determine what the player is doing (running, jumping, etc) and then do something with it later...

Code:
if (jump)
  {
  sprite_index==etc
  }
else if (run)
 {
  etc
 }
else if (walk)
 {
 etc
 }
Thanks, it worked! I had to do a LOT of messing around to get this to work. I had to make a new variable called still, which is set to true when Mario isn't moving. But in the end, it worked perfectly. Again, thanks!
 
Top