Simple Example
Home Up Event Sequence Simple Example Ramps curved surfaces Old version

 

Simple Platform Movement Example

Download: GMD EXE

Library file: GML

This uses the following script in the End Step event:

// Simple platform movement code
// Oliver Broad 2004
// This code goes in the End_Step event
//
// First test if a collision occured
// Note this version should be more reliable in games where
// collision events can modify speed and direction
// however it may lead to slightly inconsistent collision handling
// 
x=xprevious
y=yprevious
if place_free(x+hspeed,y+vspeed)
{
    x+=hspeed
    y+=vspeed
}
else
{
    //Move horizontally, testing for contact
    if hspeed<>0
    {
        move_contact_solid(180*(hspeed<0),abs(hspeed))
        if not place_free(x+sign(hspeed),y) 
        {
            hspeed=0
        }
    }
    //Move vertically, testing for contact
    if vspeed<>0
    {
        move_contact_solid(90+180*(vspeed>0),abs(vspeed))
        if not place_free(x,y+sign(vspeed))
        {
            vspeed=0
        }
    }
}        

The script checks that a move will not cause a collision with solid. If the position does overlap a solid then it first moves the object as far as possible horizontally, then as far as possible vertically.

The rest of the object's code should be self explanatory. The sprite selection code uses an array to quickly select the appropriate sprite. The left and right events are relatively complex, but only to prevent the player "moonwalking" when facing a wall, and to make the player less able to change direction while jumping.

The "Left" event in drag and drop code

The "LEFT" event:

  1. Check its possible to move
  2. Check we're not moving too fast
  3. Check we're standing on something
  4. Add to horizontal speed
  5. Set the "moving" flag
  6. Set "dir" to 0 (face left)
  7. Set Alarm 0 to 4, when alarm reaches zero "moving" flag is cleared