Simon Donkers's Game Maker site siteSimon Donkers's Game Maker site
Version: GM 5+6 Views: 9640 Script, Game Maker 5 and 6 compatible
Filesize: 5701 byte Creative Commons License

Platform movement engine

1/52/53/54/55/5 (58 votes)

A basic script to completely arrange the movement for a platform game. Very easy to use. Simply place in the step-event and fill in the required arguments.


 Platform movement engine
//////////////// Platform movement engine ////////////////////
//
// Copyright Simon Donkers 3-7-2006
// www.simondonkers.com - gmmentor@simondonkers.com
//
// the arguments are: behind is recommandable value
// argument0 is horizontal speed 4
// argument1 is jump speed 10
// argument2 is gravity 0.3
// argument3 is speed on ladder 3
// [argument4 is object ladder ? (optional)]
//
// This script will if placed in the step event
// generate a correct movement in a platform game
// all solid objects are ground
// replace //sprite_index := ...; discription with:
// sprite_index := the sprite matching the discription;
//
/////////////////////////////////////////////////////
if !keyboard_check(vk_left) and !keyboard_check(vk_right) and !keyboard_check(vk_up) and !keyboard_check(vk_down) and vspeed = 0 then
  {//If nothing is happening
  //sprite_index := ...; sprite stand still
  }
gravity := argument2;
if (vspeed >= 0 && not place_free(x,y + vspeed + 1)) then
  {//if falling and there's ground below you
  move_contact_solid(270,vspeed + 1);
  vspeed = 0;
  gravity := 0;
  }
if keyboard_check(vk_left) and (not keyboard_check(vk_up)) then
  {//if moving left and not flying
  for (i = 0; i<= 8; i += 1;)
    {//check to see if you are goin up a ramp.
    if place_free(x-argument0,y-i)
      {
      //sprite_index := ...; sprite left
      x-= argument0; y-= i; exit;
      }
    }
  }
else
  {//if moving left and flying
  if place_free(x-argument0,y + vspeed + argument2) and keyboard_check(vk_left) then
    {
    x := x-argument0;
    //sprite_index := ...; sprite flying/falling
    }
  }
if keyboard_check(vk_right) and (not keyboard_check(vk_up)) then
  {//if moving right and not flying
  for (i = 0; i<= 8; i += 1;)
    {//check to see if you are goin up a ramp
    if place_free(x + argument0,y-i)
      {
      //sprite_index := ...; sprite right
      x += argument0; y-= i; exit;
      }
    }
  }
else
  {//if moving right and flying
  if place_free(x + argument0,y + vspeed + argument2) and keyboard_check(vk_right) then
    {
    x := x + argument0;
    //sprite_index := ...; sprite flying/falling
    }
  }
if (argument[4]!=0)
  {//if argument4, stair object is given
  if place_meeting(x,y,argument4) then
    {//if on a stair
    gravity = 0; vspeed = 0;
    //sprite_index := ...; sprite climbing
    }
  if (keyboard_check(vk_up)) then
    {//if moving up
    if place_meeting(x,y,argument4) then
      {//if moving up on a stair
      if place_free(x,y-argument3) y -= argument3;
        //sprite_index := ...; sprite climbing
      }
    else
      if place_free(x,y + 1) = false then
        {//if moving up not on a stair and on the ground
        vspeed = -argument1;
        }
    }
  if (keyboard_check(vk_down)) then
    {//if moving down
    if place_meeting(x,y + argument3 + sprite_height-sprite_yoffset,argument4) and place_free(x,y + argument3) then
      {//if moving down on a stair and no ground below
      y := y + argument3;
      vspeed := 0;
      //sprite_index := ...; sprite climbing
      }
    else
      {
      if place_meeting(x,y + sprite_height-sprite_yoffset,argument4) then
        {//if moving down a stair with ground below
        move_contact_solid(270,argument3);
        vspeed := 0;
        }
      }
    }
  }
else //if argument4 (object stair) is not passed along
  if keyboard_check(vk_up) and !place_free(x,y + 1) then
    vspeed = -argument1; //jump

if (not place_free(x,y-sprite_yoffset + vspeed)) then
  {//if not place free below the object
  vspeed := argument2;
  }
if (not place_free(x,y)) and place_free(x,y-1) then
  {//if stuck just below surface
  y := y-1;
  vspeed := 0;
  }
if vspeed!= 0 then
  {
  //sprite_index := ...; sprite flying/falling
  }

Tip: Have a look at the tutorial How to execute a script

User comments

 Dank posted at 2005-06-29 21:28:40Quote
This thing is awsome, i had so much trouble trying to do the platform thing myself, but this helped alot.
 Injection posted at 2005-09-17 18:49:10Quote
wow! I think this is a bug when you go right/left on the ladder you go down.
  posted at 2006-01-23 04:05:16Quote
I tried many different platform engines but this one seems to be the simplest.Thank You.
 Alex posted at 2006-12-19 21:46:14Quote
You, sir, are awesome.
 Zac1790 posted at 2007-01-09 22:01:42Quote
The ladder thing never worked for me, wierd... The nicest feature is the slant movement.
 Jock posted at 2007-02-25 17:01:51Quote
Awesome! Really helped me out.
 GM NERD posted at 2007-03-09 09:59:28Quote
GREAT!!
 Potato posted at 2007-03-25 01:26:41Quote
Wonderful. Only thing I don't like is when moving down a slope, you don't slide along it like normal, but kinda bounce downward.

But I use it anyway! So simple!
 anyonamus posted at 2007-04-28 01:01:00Quote
very nice but is there any way to stop it from makign you go downwards when you go onto a slope thats like this /
not sure if its a sprite issue aor a code issue
 HELP ME posted at 2007-05-11 22:07:40Quote
Im a newb at this.....so i kinda dont know how to actually make it work...im sooo sad.
 Ragehunter posted at 2007-05-19 20:40:30Quote
Is awesome!, really amazing, but if i wanna move with other keys like z,x,c or d . How i can write them in the script, becuase i already tried keyboard_check(vk_z), for example, and doesn`t happen anything, please help me , i need it for my multiplayer game.
 Help! posted at 2007-06-17 18:21:01Quote
I keep getting this message. What should I do
COMPILATION ERROR in Script: Platform movement
Error in code at line 5:
gravity := argument2;

at position 10: Variable name expected.
I'm not sure what the variable name should be, as I already have put in variables for gravity and vspeed.
 Help! posted at 2007-06-18 04:04:51Quote
nevermind i found the problem, now it says i need an assignment operator. I'm sure I'll find it tho at some point. Thanks for the script tho its better than having to make one myself.
 Luke posted at 2007-07-25 18:26:27Quote
This is fantastic. iv realy struggled myself when trying to create code for a platform game, but (and here's the but) the jump/falling image i have used two of them, one right one left and it always displays the one facing right! help please!
 Luke posted at 2007-07-25 18:32:54Quote
Sorted it, dont worry! i changed some of the settings to image_speed= 0 and now it works just the way i needed it to. didnt notice some stuff at the bottom of the code! hehe
Thanks all!
 Help! posted at 2007-08-12 05:13:58Quote
I use game maker alot, but I could never figure out the platform motion. I put execute script in the step event of my player object but when I play the game nothing happens! :( Help me when you can please.
 mmmmm posted at 2007-08-12 08:48:59Quote
this is copyrite looks alot like MY code there about 80% witch i done when gm 6 first came out
 Simon Donkers posted at 2007-08-12 20:19:35Quote
This script is loosely based upon the platform tutorial from Mark Overmars. Almost any platform game that people have made is programmed in a similar manner.

I can assure you that I never stole or even looked at your code before and spamming my reply form won't help you get anywhere other then banned.
 HELP posted at 2007-12-01 03:56:25Quote
i get this error!
"COMPILATION ERROR in Script: script3
Error in code at line 122:

}

at position 4: Symbol } expected"
what do I do?
 help posted at 2007-12-01 04:01:35Quote
i fixed that last one, but now i get these errors
"Error in action number 1
of Step Event
for object player:

Error in expression:horizontal speed 4
position 1: Unknown variable horizontal"
and
"Error in action number 1
of Step Event
for object player:

Error in expression:jump speed 10
position 1:Unknown variable jump"

Please tell me what do to
 Dot posted at 2007-12-10 17:29:44Quote
Is there a way to detect whether the object is falling or jumping and give different sprites to them ?
 help me posted at 2007-12-30 14:11:30Quote
I keep getting this 4 the goin up slope 1
___________________________________________
FATAL ERROR in
action number 1
of Collision Event with object object14
for object object0:

COMPILATION ERROR in code action
Error in code at line 1:
{if place_free(x-argument0,y-i)}

at position 32: Unexpected symbol in expression.
 Omg posted at 2008-01-18 02:02:58Quote
How do you fill these "arguments"
I've tried putting

//argument0
horizontal speed = 4
//argument1
jump speed = 10
//argument2
gravity = 0.3

in the create event, but I'm really not sure what you consider "arguments" and what you want us to do with them.
Kudos for putting effort into making a script like this though. They sure do take a lot of work ;D
 Re: Omg posted at 2008-01-18 02:30:29Quote
Ok I figured it out thanks for the article, It helped me out a ton as I've never used arguments before lol
 Snail posted at 2008-02-18 23:42:37Quote
This is awesome. Completely awesome.

My only complaint is, can you make one that utilizes hspeed and friction, so the character can slide around a bit before stopping? It just looks better that way. Thanks.

Also, what does := do? Is it the same as the = used for setting a variable?
 Sam posted at 2008-04-01 22:29:44Quote
Hi, great script but i have one problem. I set the player to face right when nothing is happening so after just walking left when i stop he turns around, is there a way to fix this ?
 Scruffy posted at 2008-04-04 22:42:28Quote
___________________________________________
ERROR in
action number 1
of Step Event
for object obj_falling:

Error in code at line 1:
if falling= true{gravity = 0.5}else{gravity = 0}

at position 4: Unknown variable falling

___________________________________________
ERROR in
action number 1
of Step Event
for object obj_falling:

Error in code at line 1:
if falling= true{gravity = 0.5}else{gravity = 0}

at position 4: Unknown variable falling

___________________________________________
ERROR in
action number 1
of Step Event
for object obj_falling:

Error in code at line 1:
if falling= true{gravity = 0.5}else{gravity = 0}

at position 4: Unknown variable falling

___________________________________________
ERROR in
action number 1
of Step Event
for object obj_falling:

Error in code at line 1:
if falling= true{gravity = 0.5}else{gravity = 0}

at position 4: Unknown variable falling

___________________________________________
ERROR in
action number 1
of Step Event
for object obj_falling:

Error in code at line 1:
if falling= true{gravity = 0.5}else{gravity = 0}

at position 4: Unknown variable falling

___________________________________________
ERROR in
action number 1
of Step Event
for object obj_falling:

Error in code at line 1:
if falling= true{gravity = 0.5}else{gravity = 0}

at position 4: Unknown variable falling
 Viski posted at 2008-04-27 11:03:51Quote
Where must i write the name of sprite?
//sprite_index := ...; sprite stand still

Do I replace the sprite_index with the name of the sprite?
 Mario & Luigi are my cousins posted at 2008-07-03 23:16:45Quote
So cool. too cool. way too cool. you need to take some of the coolness away. Just too cool. AWESOME!!!!!!!!
 yeayeayea posted at 2008-07-25 18:44:58Quote
dosent work
but still good work

Reply

Name:
Site: