Ladders

Written by Luke Harrison 2007

In this tutorial you will learn how to make ladders that your character can climb.


Summary

  1. Create a Ladder Object
  2. Create a Variable to Check if Character is on Ladder
  3. Stop Gravity if Character is on Ladder
  4. Stop Character Moving When on Ladder
  5. Stop Character Jumping when Climbing the Ladder
  6. Move Character Up and Down the Ladder

Download Example


Before you Start

You will need the following:

  • Platform game engine which includes a character that is pulled down by gravity and a wall.
  • A sprite for a ladder, if you don't have one, download mine, here

If you don't have a platform engine, follow this tutorial or download the example, Platform Movement


Create a Ladder Object

You need to create a Ladder Object,

  • Add a New Object
    • Set the name as o_ladder
    • Set the sprite as your ladder sprite

Create a Variable to Check if Character is on Ladder

You need to create a variable that either be true or false, depending if the user is on the ladder or not. The reason you need a variable for this is to make it easier to determine if the character should climb or jump.

  • Open the Object Properites for the Character Object

  • Open the Create Event, if there isn't one, Add a Create Event
  • Add the Action, Set Variable from the Control Tab
    • Set Variable as, on_ladder
    • Set Value as, false

When the Character Object is created, we set the on_ladder variable as false by default.


Stop Gravity if Character is on a Ladder

When the Character is on a Ladder, it should stay where it is unless a key is pressed to move it up or down. We need to stop gravity setting on the step event because when on the ladder, the space under the character will be empty which would normally set gravity.

  • Open the Character Step Event

  • Add the Action, If Variable has Value at the TOP of the Action List
    • Set Variable as, on_ladder
    • Set Value as, false
    • Set Operation as, Equal To

The Reason we add this to the top is so it is the first check before setting gravity, first the script will check if the character is NOT on a ladder, if it isn't it will then check if the character is in mid air. If the Character is NOT on a ladder and if it is in mid air, then, graivty will be set.

  • Add the Action, Set Variable at the BOTTOM of the Action List
    • Set Variable as, on_ladder
    • Set Value as, false

This Event adds gravity if the character isn't on a ladder or in mid air. After the Step, it sets on_ladder as false. The variable. on_ladder will be set as true on a collision event with the ladder.


Stop Character Moving when on a Ladder

When the character collides with the ladder, you want to stop the character falling down and set the on_ladder variable to true.

  • Add a Collision Event with the Ladder

  • Add the Action, Set Variable
    • Set Variable as, on_ladder
    • Set Value as, true
  • Add the Action, Set Vertical Speed from the Move Tab
    • Set Vspeed as, 0

Setting the vspeed to 0 will stop the charater moving up or down if it was previously jumping or falling.


Stop Character Jumping when Climbing the Ladder

When the Character is on the ladder, they shouldn't be able to jump when moving up or down. This will happen because the Up Key Press Event

  • Add the Action, If Variable has Value at the TOP of the Action List
    • Set Variable as, on_ladder
    • Set Value as, false
    • Set Operation as, Equal To

This If statement will prevent the character jumping if they are on the ladder.


Move Character Up and Down the Ladder

When the character is on the ladder, the up and down keys should move the character up or down the ladder.

  • On the Chracter Object, Add a Keyboard Up Event

  • Add the Action, If Variable has Value from the Control Tab
    • Set Variable as, on_ladder
    • Set Value as, true
    • Set Operation as, Equal To

  • Add the Action, Jump to Position
    • Set Y as, -5
    • Tick Relative

This code will move the Character up the ladder if they are on it which is determined by the value of the on_ladder variable.

  • Repeat this for a Keyboard Down Event
    • The Y value on Jump to Position for Down should be 5 Relative.