So I have ledges in my game like any other platformer. problem is when I am on the very edge of that ledge and am about to fall off, as depicted in the attached video, the character shakes uncontrollably. I was told to do :
if ( this.__hero.body.velocity.y > x ) { } where 'x' is a number > 0
but that has a slight delay before the fall animation starts playing.
How do I detect edges & put a stop to this bug once & for all?
I don’t think detecting edges will solve the problem. You may have a race condition where changing the body is changing the collision state, which is changing the body and so on.
From the video it looks like the body is cycling between 3 states. 2 of those appear with the same animation frame, the “standing” one. In one state the body is farther to the left. That one is definitely overlapping the ledge.
@samme : if ( this.__hero.body.velocity.y > 100 ) { } seems to work, but the problem is that makes it so the falling animation takes 1 second before it activates. How do I fix this?
I’m not sure what could possibly be the cause without having more details, but from rewatching the video you attached, does the sprite ever “leave” the bounds behind when your character runs left or right? I noticed from pausing at the beginning of the video that when you moved your player to the right, the bounds was “left behind”, then it adjusted to the player, but maybe that occurred because there was no collision under the player.
There’s not enough information here to identify your problem for certain. My best guess is there is some kind of feedback loop going on among the animation state changes, the physics body, and the ledge collision.
The body’s position can change
whenever the sprite’s displayWidth or displayHeight changes, such as when changing texture frames
when you call body.setSize()
when you change the body’s position or offset
when you change the sprite’s position
So it could be any of those.
For a workaround, go back to
if ( this.__hero.body.velocity.y > 100 ) { … }
and find the smallest value that works.
Or always keep the body the same size when changing frames: