Animation Issue w/ Platformer Game

Hey all! Been scrapping together a quick platformer to prototype. I’m using the Defold Platformer example and have only changed out the sprites. Problem is the player script keeps switching from different animation states.

You can see the issue here.

I think the issue might be in some where in this block

local function update_animations(self)
	-- make sure the player character faces the right way
	sprite.set_hflip("#sprite", self.move_input < 0)
	-- make sure the right animation is playing
	if self.ground_contact then
		if self.velocity.x ~= 0 then
			play_animation(self, anim_run)
		else
			play_animation(self, anim_idle)
		end
	else
		if self.velocity.y ~= 0 then
			play_animation(self, anim_jump)
		else
			play_animation(self, anim_fall)
		end
	end
end

Appreciate any suggestions, thanks!

This is a common issue with platformers and is to do with the way the collision correction code works. Print your variable self.ground_contact to the console and it probalby flicks between true and false all the time.

A usual solution would be to have a short count-down timer instead of a flag. Reset the timer when ground contact is detected. This has the added benefit of allowing so called “coyote time” to be implemented.

6 Likes

That’s actually clever! I haven’t even considered that you’d get this for free then! :+1:

2 Likes

Will give this a try, thanks so much!