I am trying to figure out the best way to create an animation with multiple states but I can't seem to find any examples on a clean way to achieve what I'm after.
I have a sprite sheet for a character that has two states of jumping animations, there are 6 frames where the character animation is essentially jumping off the ground.
There are another 6 frames where the character is in a "jump loop" so the character is already off the ground but the arms etc move slightly.
When I put all the images into a list and iterate through them the initial part of the jump looks fine because the character gets off the ground and then goes into the jump loop. But once the jump loop sequence of frames is done the animation goes back to the start mid air, so it looks as though the character is just jumping off something.
The code for the function I have thus far is as follows
def animate(self):
now = pg.time.get_ticks()
# Jumping
if self.jumping:
if now - self.last_update > 18:
self.last_update = now
if self.left:
self.current_frame = (self.current_frame + 1) % len(self.jump_l)
bottom = self.rect.midbottom
self.image = self.jump_l[self.current_frame]
self.rect = self.image.get_rect()
elif self.right:
self.current_frame = (self.current_frame + 1) % len(self.jump_r)
bottom = self.rect.midbottom
self.image = self.jump_r[self.current_frame]
self.rect = self.image.get_rect()
self.rect.midbottom = bottom
The animation works but essentially what I want to do is to only how the first 6 frames and then loop over the last 6 frames until the character lands.
I would recommend splitting up the animation Spritesheet so you have one row with the six frames for jumping up, and then another row for the other six falling frames.
Once you can do that, just check if your sprites velocity variable is positive or negative like this:
if self.vel > 0:
# Switch animation frames to jumping up
elif self.vel < 0:
# Switch animation frames to falling down