GML Platformer Mechanics Made Easy

The following is a simple bit of script which gives a game similar mechanics and physics to classic platformers like Mario or Mega Man, just add your own solid objects!  Eventually, I’ll repackage my physics as a stand alone function so that it can be applied to random environment objects or enemies.  This code can reworked to run in any language, just replace move_contact_solid with a function containing a for loop that looks for collisions at an increasing distance then moves the player flush with the other object when it finds a collision.  Gravity can be replaced with a variable that increased exponancially (its up to you).  place_free is a simple collision checking function that will come with whatever game engine you may be useing.  Of course, there are plenty of other ways to handle this sort of thing, but my goal was to post something simple.

In the player object Create Event:  Set these variable to control the behavior of your character.

moveSpeed = 5
jumpSpeed = 7
gravSpeed = 0.5
vspeedLim = 10

In the player object Step Event:  Here are the mechanics to make your game fun.

//movement
if keyboard_check(vk_left) {
if place_free(x-moveSpeed,y) then x -= moveSpeed else move_contact_solid(180,moveSpeed)
}
if keyboard_check(vk_right) {
if place_free(x+moveSpeed,y) then x += moveSpeed else move_contact_solid(0,moveSpeed)
}

//gravity
if place_free(x,y+vspeed+1) then gravity = gravSpeed else { gravity = 0; vspeed = 0; move_contact_solid(270,vspeed+2) }
if vspeed > vspeedLim then vspeed = vspeedLim

//jump
if keyboard_check(vk_up) and not place_free(x,y+1) then vspeed -= jumpSpeed

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s