Part 3: Ledge grabbing
Lots of platformers have Ledge grabbing. So I've worked on a clean way to do so, however, you cannot do one without having another sprite following you.
Here's what I did:
Seems quite complicated right?
Let's explain it
Set up the Ledge Sprite
Here you create a sprite that has the platformer behavior (dont forget to disable default controls for it).
You make it spawn on the player's image point number 1 which you have to set above the player (-5 on the Y axis for exemple). Don't forget to center the point on the X axis. It's important. Then, you set it's width to the Player's width. You can set whatever you want for it's height, but the lesser, the better.
Then you update the LedgeGrabber's position by setting it to the player's X. For the Y, either you can set it to the Point 1's Y, or you can use the following formula:
This adds a smooth transition, so if the player is falling, it becomes easier to grab ledges.
Basically, if the player has a wall to its left or to its right, then check if the LedgeSprite has one too.
As it's above the player's head, if it doesn't, then there is a Ledge.
If the LedgeSprite neither has a wall to its left nor to its right, then set the gravity to 0
Else, set the gravity to its default value.
Also if the player neither has a wall to its right or its left, then, set the gravity back to its default value too.
Here, if the player is jumping and the all the Ledge grabbing conditions are verified, then make the player slowly get up the ledge.
The deceleration part is just here to avoid shakies when the player reaches the top.