One of the first things we need to look at is how to give our character platform movement.  It should move left or right when the left or right arrow keys are pressed.  It should jump when the up arrow key is pressed and there should be gravity, so it should fall when it is not standing on a platform.

In this section we will look at left, right movement and gravity.

We are going to make our character move by changing its coordinates.  By changing the x coordinate it can be made to move horizontally.

 

Create Sprites and Objects

First we will create a character and a few blocks to make a platform to walk on.  Add in the following: sprPerson, sprPlatform  (I got my sprites from the Pacman and breakout folders.)

 

  Now create objPerson and objPlatform and attach your sprites to them.  (Don't forget to make your platform solid.)

Place them in the room

 

Left & Right Movement

Open the properties for your person object.  Add a left keyboard event.  Check if the Relative position (-4,0) is collision free (Control tab).   If it is then jump to that position.

Why must we tick relative for the position?  
  Now add an event for the right keyboard, but use coordinates (4,0) relative instead.

Gravity

We expect that when our character walks off the end of the platform, it should fall.  To do this we need to add in gravity.

Gravity needs to operate all the time when our character is in the air.  We will add it into the Step event for objMan.  Set it to be in direction 270 (downwards) with a value of 0.5.

Problem 1

Straight away you can see we have a problem!  Our character goes straight down through the platform.

We need to add in two actions for when it collides with the platform. 

  • We will set the vertical speed to be 0,

  • We will move it to contact the platform.

 

Problem 2

We now have the problem that the pull of gravity prevents us being able to move our character along the platform.  We will change our Step event to only have gravity if the character is not on a platform

We can check the position 1 pixel below the objMan to see if it is collision free (that is there is no platform).  If it is collision free, we set the gravity, otherwise we set the gravity to be 0.

 

So now our character can move left and right and gravity will cause it to fall if it steps off a platform.  Next we need to move onto jumping.