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

Platform movement engine

1/52/53/54/55/5 (110 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
 konker posted at 2008-09-14 07:03:07Quote
how would I go about adding double jumping to this?
 vredy posted at 2009-01-01 12:52:16Quote
how does it works
 Dylan posted at 2009-01-29 21:24:19Quote
I just dont get it, how the **** do i get it working?? (i am a a total noob on everything that has something to do with scripts....)
 Fra÷t posted at 2009-03-03 17:07:12Quote
It doesn't work, when I start my game Zero stands on the air (I'm making a Megaman Zero game)
The gravity doen't work here... it can't move either
 Mauritsz posted at 2009-03-04 18:17:18Quote
Ik zocht naar gravity waarmee op hellingen kan lopen en dat heb ik nu gevonden. Bedankt Simon :D
 XD posted at 2009-03-26 21:56:14Quote
XDXDXD
 Purianite posted at 2009-04-17 22:02:11Quote
Going down ramps seems to be rather rough, but up is fine. Any way to fix it? My friend told me to check for down before forward.
 Kira Games posted at 2009-05-30 04:58:50Quote
Lets see if I can help with a few of these qs.

How do I use this?

Start off in Advanced Mode in GM7. Then go to resources and select "Create Script". Copy and paste this code into the notepad and give it a name. Click check mark.

Now, double click you character's object and click "Add Event". Select "Step" and "Step". Click on the "control" tab over there on the right and in the category "Code" drag the piece of paper with the "play" symbol over to the Action section. A window opens. Select the script, and underneath it replace all the arguments with the arguments he gives you in the comment of this script. Done.

How do I change the "Up" button into a letter (Z will be the example)?

Replace "vk_up" with "ord("Z";)" (without the outer quotations).

That's all I know for now...
 Yekolf posted at 2009-06-29 15:38:42Quote
this might not be the place to ask, but could you please include somesort of tutorial on how to incorporate jumpthroough platforms using this engine, im finding it very hard.
 wha... posted at 2009-12-24 12:00:25Quote
why can't i move?
 Mike posted at 2010-01-13 02:15:30Quote
Ok for those of you with problems:

1.
You have to name all the arguments as variables in create object.

2.
You must read the captions and insert the sprites for your character.

To the writer of this script: Thank you, this has been very helpful.
 delibrete posted at 2010-04-28 09:05:34Quote
Thank you so much Simon! this is going to help me a lot with a game i'm trying to make.

And don't worry, i'll make sure i'll attribute your work in my game (I read the license).

Thanks!
 Ben posted at 2010-05-05 21:10:56Quote
very awsome!!!!!!!!!!!!!
 Rene posted at 2010-09-30 17:14:42Quote
It want work why?
 Rene posted at 2010-09-30 17:17:48Quote
I mean my monster going amok when i activate that script ...
Yes it is under the step event dont worry but still it are not working ...
WHY ???
 MBeentjes posted at 2010-10-31 19:34:18Quote
Really good working platform movement engine. I will use it in platform games! :D


MBeentjes.
You'll know me from NGMC ;)
 MBeentjes posted at 2010-10-31 19:35:17Quote
Really good working platform movement engine. I will use it in platform games! :D


MBeentjes.
You'll know me from NGMC ;)
 Haydan Titus posted at 2010-12-14 16:57:41Quote
Thanks dude. i found this thing like 4 years ago but it iz still da best!!! by the way u should start updating this again
 jester-n001@mymail.mssu.edu posted at 2011-01-04 06:55:35Quote
don't put gravity in your move engine, if you have a script designated to gravity, you can apply it to other objects, then again, I guess it would probably run fastest if you just had both
 twoek posted at 2011-01-05 11:33:38Quote
bami nasi
 MBeentjes posted at 2011-01-31 16:57:17Quote
I'm using it in Totem Islands. :D

Reply

Name:
Site: