Jumping Animation

J

Jessiker

Guest
I'm sure this has been answered a million times, but I don't have time to go through all these threads...

Anyways, I want my sprite to have a jumping animation and a falling animation. However, both of the sprites are separate (one falling, one jumping). The falling sprite is the only one that has the animation. Besides that, I also added an idle animation and a running animation. I followed a tutorial but unfortunately it didn't explain to me what I should do if I have more than one frame for my sprite.


Here is the line of code that I used:

//Main Animations

if (!place_meeting(x, y+1, JURRASIC_GROUND)) or (!place_meeting(x, y+1, JURRASIC_GROUND_UP))
{
sprite_index = Jacklynn_Jumping_BARE;
if (sign(vsp)> 0)
{
image_speed = 1;
sprite_index = Jacklynn_Falling_BARE;

}
else
{
sprite_index = Jacklynn_Jumping_BARE;
image_index = 0;
}
}
else
{
if (place_meeting(x, y+1, JURRASIC_GROUND)) or (place_meeting(x, y+1, JURRASIC_GROUND_UP))
{
if (hsp==0)
{
sprite_index = Jacklynn_Standing_BARE;
image_speed = 1;
}
else
{
if (hsp==8)
{
sprite_index = Jacklynn_Skating_BARE;
image_speed = 1;
}

}
}
}

For some odd reason, my character refuses to do her running and idle animations. It could be because of all of the "else" statements, but I have no idea. Also, if this is of any importance, here are the frames for each animation:
  • Falling animation: 4 Frames
  • Jumping animation: 1 Frame
  • Idle animation: 8 Frames
  • Skating/ Running animation: 10 Frames
  • Hurt animation (I may pester this forum about how to do hurting animation later): 1 Frame

Credit for helping me with most of the code goes to Shaun Spalding!
 
Last edited by a moderator:
I'm sure this has been answered a million times, but I don't have time to go through all these threads...

Anyways, I want my sprite to have a jumping animation and a falling animation. However, both of the sprites are separate (one falling, one jumping). The falling sprite is the only one that has the animation. Besides that, I also added an idle animation and a running animation. I followed a tutorial but unfortunately it didn't explain to me what I should do if I have more than one frame for my sprite.


Here is the line of code that I used:

//Main Animations

if (!place_meeting(x, y+1, JURRASIC_GROUND)) or (!place_meeting(x, y+1, JURRASIC_GROUND_UP))
{
sprite_index = Jacklynn_Jumping_BARE;
if (sign(vsp)> 0)
{
image_speed = 1;
sprite_index = Jacklynn_Falling_BARE;

}
else
{
sprite_index = Jacklynn_Jumping_BARE;
image_index = 0;
}
}
else
{
if (place_meeting(x, y+1, JURRASIC_GROUND)) or (place_meeting(x, y+1, JURRASIC_GROUND_UP))
{
if (hsp==0)
{
sprite_index = Jacklynn_Standing_BARE;
image_speed = 1;
}
else
{
if (hsp==8)
{
sprite_index = Jacklynn_Skating_BARE;
image_speed = 1;
}

}
}
}

For some odd reason, my character refuses to do her running and idle animations. It could be because of all of the "else" statements, but I have no idea. Also, if this is of any importance, here are the frames for each animation:
  • Falling animation: 4 Frames
  • Jumping animation: 1 Frame
  • Idle animation: 8 Frames
  • Skating/ Running animation: 10 Frames
  • Hurt animation (I may pester this forum about how to do hurting animation later): 1 Frame

Credit for helping me with most of the code goes to Shaun Spalding!
First of all, in your case, just make
image_speed = 1
In the create event, since you aren't changing it anywhere. Game maker will do the work of resetting image index to 0 when the sprite changes for you.

Second, I don't know what your ground up is, but if it's an object that you can stand on, or anything that would make the character not do her jump or fall animation,
*Change the or in the first check to an and
When you put an or there, even if she is touching a ground object, if she is not touching a ground up once at the same time, she will do her jump or fall animation. When you have an and there, she will only do a jump or fall animation when she is not touching both a ground object and ground up object. So just change that or to an and.

Next, your very first else {} block should be removed with everything in it, the one closest to the top. This is because it is unnecessary. You made the default sprite, a jumping sprite when your character it's in the air. Then when the character is going down, the default sprite changes to the falling sprite, you don't need to check if that is false because the other sprite while in the air is already set, and therefore it is redundant to have that else block.

Then, this check that you do after the else statement should be removed
if (place_meeting(x, y+1, JURRASIC_GROUND)) or (place_meeting(x, y+1, JURRASIC_GROUND_UP))P
An else statement basically says that if the if statement was false, do this. So you don't need to check the opposite case again because the else statement already does that. So just remove those 2 lines and the 2 brackets that belong to it, {}, leave the code that was inside there.


I think that my second and last paragraphs would solve your problem, but the other two are important to optimize and make it easier to deal with the code
 

flerpyderp

Member
Game maker will do the work of resetting image index to 0 when the sprite changes for you.
This isn't true. Although in this particular case, since there is only one frame for the jump sprite, it will indeed be set to 0 by the time the falling sprite is used. But, if the character was running then fell off a ledge or something, the falling animation would not automatically play from the first frame.
 
Last edited:
J

Jessiker

Guest
Thank you so much for the replies! It worked out perfectly, besides the falling animation... But I think I'll try and figure that out on my own.
 
Last edited by a moderator:
J

Jessiker

Guest
I did everything you said, and for the most part everything worked out perfectly, so thank you SO much! All the animations are running just fine. But for some odd reason the falling animation will NOT work.

Here is all the code that I used for the "step" process:

//Collisions
//HORIZONTAL
if (place_meeting(x+hsp,y,JURRASIC_GROUND))
{

while (!place_meeting(x+sign(hsp),y,JURRASIC_GROUND))
{
x = x + sign(hsp);
}
hsp = 0;

}
if (place_meeting(x+hsp,y,JURRASIC_GROUND_UP))
{

while (!place_meeting(x+sign(hsp),y,JURRASIC_GROUND_UP))
{
x = x + sign(hsp);
}
hsp = 0;

}
x = x + hsp;
//VERTICAL
if (place_meeting(x,y+vsp,JURRASIC_GROUND))
{

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

}
if (place_meeting(x,y+vsp,JURRASIC_GROUND_UP))
{

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

}
y = y + vsp;

//Movement and Input
key_right = keyboard_check(vk_right);
key_left = keyboard_check(vk_left);
key_jump = keyboard_check(vk_space);

var move = key_right - key_left;

hsp = move * wlkspeed;

vsp = vsp + grv;

if (place_meeting(x, y+1, JURRASIC_GROUND)) and (key_jump)
{
vsp = -11.9;
}

if (place_meeting(x, y+1, JURRASIC_GROUND_UP)) and (key_jump)
{
vsp = -11.9;
}


//Main Animations

if (!place_meeting(x, y+1, JURRASIC_GROUND)) and (!place_meeting(x, y+1, JURRASIC_GROUND_UP))
{
sprite_index = Jacklynn_Jumping_BARE;
image_speed = 0;
if vsp > 0
{
sprite_index = Jacklynn_Falling_BARE;
image_speed = 1;
}

}
else
{
if (place_meeting(x, y+1, JURRASIC_GROUND)) or (place_meeting(x, y+1, JURRASIC_GROUND_UP))
{
if (hsp==0)
{
sprite_index = Jacklynn_Standing_BARE;

}
{
if (hsp==8)
{
sprite_index = Jacklynn_Skating_BARE;

}

}
}
}

The animation may not be working because I may have something in my main code but I really have NO idea what it could be. I removed the "sign" from the "Main Animations" because it was unnecessary. And yes, both the JURASSIC_GROUND_UP and JURASSIC_GROUND are objects the sprite stands on.
 
Last edited by a moderator:

flerpyderp

Member
Code:
if (!place_meeting(x, y+1, JURRASIC_GROUND)) and (!place_meeting(x, y+1, JURRASIC_GROUND_UP))
{
sprite_index = Jacklynn_Jumping_BARE;
image_speed = 0;
if vsp > 0
{
sprite_index = Jacklynn_Falling_BARE;
image_speed = 1;
}

}
You need to put
Code:
sprite_index = Jacklynn_Jumping_BARE;
image_speed = 0;
in an else statement after
Code:
if vsp > 0
{
sprite_index = Jacklynn_Falling_BARE;
image_speed = 1;
}
Like so:

Code:
if vsp > 0
{
sprite_index = Jacklynn_Falling_BARE;
image_speed = 1;
}
else
{
sprite_index = Jacklynn_Jumping_BARE;
image_speed = 0
}
Currently, both the conditions required to make the sprite change to either jumping or falling are true while the player is falling in the air. So, even though the vsp may be greater than 0, the condition for changing to the jumping sprite is also true because there is currently no ground directly below the player. So the sprite is repeatedly changing between jumping and falling while the player is in the air.
 
J

Jessiker

Guest
Code:
if (!place_meeting(x, y+1, JURRASIC_GROUND)) and (!place_meeting(x, y+1, JURRASIC_GROUND_UP))
{
sprite_index = Jacklynn_Jumping_BARE;
image_speed = 0;
if vsp > 0
{
sprite_index = Jacklynn_Falling_BARE;
image_speed = 1;
}

}
You need to put
Code:
sprite_index = Jacklynn_Jumping_BARE;
image_speed = 0;
in an else statement after
Code:
if vsp > 0
{
sprite_index = Jacklynn_Falling_BARE;
image_speed = 1;
}
Like so:

Code:
if vsp > 0
{
sprite_index = Jacklynn_Falling_BARE;
image_speed = 1;
}
else
{
sprite_index = Jacklynn_Jumping_BARE;
image_speed = 0
}
Currently, both the conditions required to make the sprite change to either jumping or falling are true while the player is falling in the air. So, even though the vsp may be greater than 0, the condition for changing to the jumping sprite is also true because there is currently no ground directly below the player. So the sprite is repeatedly changing between jumping and falling while the player is in the air.
Thank you! It worked! I'm not sure why I didn't think of that beforehand, thanks!
 
Top