I didn’t get to fiddle with the handle_geometry_contact() function as I found out that you are having an issue with the tilemap collision. Rectangular tiles in defold don’t get automatically merged into one collision object, instead each individual tile is an individual collision object, meaning that when you are walking on the ground, you are not walking on a single big rectangle, but on many smaller rectangles.
And your character ever so slightly touches the left and right sides of those individual rectangular collision objects, resulting in strong horizontal normals, that glitch your character animation loop.
There is no single “right” solution how to solve this issue. You can use a different body layout (legs collision sphere at the bottom to detect falling, left and right collision box on the sides to prevent walking through walls), you can add more checks to the code to filter those bad normals.
My personal recommendation is to use Tiled editor to draw your levels and use “object layer” to draw collision boxes on the level. In Tiled you can draw a single collision rectangle over the entire ground so you would not get those wrong normals.
I’ve made a proof of concept Tiled Lua format loader for Defold. However it might be difficult to work with if you are new to Defold.