This is yet another tutorial on making a ladder-style sprite that can be climbed in a platform-style game. Sample code and an example capx file are provided.
Controls and Events
* If you want to use the Up Arrow key to cause your player to climb ladders, you will need to disable the default platformer controls (since the Up Arrow defaults to the Jump action), and simulate the platform controls in your event sheet.
* When you overlap a ladder tile, you can change the Player's position using either "Set Y" or "Move at Angle". Either way, you will need to set the platform gravity to 0; otherwise, the player sprite will be pulled down through the ladder while it is trying to climb up.
* You also will need to reset the platform gravity when you are not actively climbing. The code below shows how to implement this using Else-blocks. Whatever solution you adopt, you need to make sure that at the very least, gravity is reinstated as soon as you are not overlapping the ladder, otherwise the player's next jump action could be too high, possibly infinitely high.
* Another frequently desired feature is that the player be able to "sit" or remain stationary on the ladder mid-way through a climb. My preferred solution for this is to assign the ladder sprites the jump-through behavior, and make the image for each of the sprites short, only containing the sides and a single rung at the top. This may not allow the player to stop at any pixel on the ladder, but they can sit on any given rung, which I find sufficient in my games. This also allows the player sprite to jump from rung to rung on the ladder, which is also fine with me. As an added bonus, using the jump-through behavior on your ladder stops the player from "bouncing" once they reach the top of the ladder, as they have something to stand on.
("Bouncing" in this context means that once the player sprite moves past the top of the ladder, gravity is reset, and if they are not on a solid platform then the player sprite will fall down; however, if the Up arrow key is being held down, as soon as the player sprite is overlapping the ladder they will continue to move up again. This cycle repeats itself, and it looks as if the player sprite is rapidly moving up and down by a single pixel.)
* To allow the player to climb back down the ladder when they are standing on top of it, for the event condition you can check if the player is overlapping the ladder at an offset; a Y offset of 2 pixels will check if the ladder is directly underneath you.
In order for the player to be able to climb up to a higher level, they need to either:
* pass through the ground, or
* there needs to be a gap in the ground where they climb up.
A variety of methods are illustrated in the diagram below, which is a screenshot of the example capx file provided for this tutorial. Both methods have their pros and cons.
Passing through the ground
To pass through the ground, two possible options are:
* Use the jump-through behavior for the ground rather than the solid behavior. However, this will cause a problem if your player sprite is able to walk into the ground sprites from the left or the right -- they will be able to pass through, which may be undesirable. (This is illustrated by ladder 1 in the capx demo.)
* Disable the solid behavior while climbing and enable it when you are finished climbing. However, this method is complicated and requires many more events than shown above in the example to process correctly, otherwise your player could get stuck in the wall.
Creating a gap in the floor
The gap-in-the-floor method is easier to implement (but it rules out ladder setups such as ladder 4 in the capx demo). There may be problems when your player is running past a ladder coming out of the floor, unless the ladder has the jump-through behavior and is aligned correctly with the ground sprites (as in ladder examples 2 and 5). When the ground is at an angle, such an area may be tricky for the player sprite to smoothly navigate unless your sprites are aligned precisely.
If your ground-sprites are misaligned and they have the jump-through behavior, then you might fall through the ground (as in ladder example 3), or if they have the solid behavior then you might bump into the ground, stop, and have to jump up to keep going (as in ladder example 6).
A note of caution: if you use ground sprites with the jump-through behavior in combination with ladders, it is possible to fall through the ground by climbing down at the bottom of the ladder (as in ladder examples 1 and 3); if you want to prevent this behavior, you can place a solid sprite overlapping the ground at the base of the ladder (as in example 2).
Personally, I prefer the setup of ladder example 5 in the demo, but depending on your game, you may want to use something else, but there are lots of factors to take into consideration. I hope this article has given you some insight, and good luck with your game creations!