Jump to content

Free Platformer code


Recommended Posts

Hi all, 


I was having a look at some code for a platformer and decided I didn't like the way

the character locked on to the platform before having finished jumping.


So I made my own code,  Woo Hoo!


In my version I use the createjs class for my bitmap images, other than that, it's just js.


I've used 'base' , 'base2', and 'base3' as platforms - same image - just scaled and loaded

with different id's - but do it your way - doesn't matter. Whatever image will work.


You can determine the width and height of an image apart from it's actual width and height.

So say you have a platform image 10px height but half of that image is a drop shadow.

Simply call the the image; myImage.height = 5; then your character won't bang it's head

on a drop shadow. X and Y values for platforms are recorded just where you put them.


In the below code there is no platform that shares the same same y co-ordinate but that would be easy.


if (character.y < platform1.y && character.x > platform1.x + 400 && character.y < Platform2.y){   //plus 400 is width of platform 1

platform2 = path}  /// DON"T SHOOT ME / HAVEN'T TRIED THIS YET.


first make a path...

function path(width, height, x, y){	this.width = width;	this.height = height;	this.x = x;	this.y = y;}

easy.   Then.....

if(helpmeghost.y < base.y){		path = base;}elseif(helpmeghost.y > base.y &&    helpmeghost.y < base2.y){		path = base2;}elseif(helpmeghost.y > base.y &&    helpmeghost.y > base2.y &&    helpmeghost.y < base3.y){		path = base3;}

Wow! Basic.

		vy+=gravity;      //apply gravity to downward motion					if (inAir){    //if not touching platform apply gravity to ghost			helpmeghost.y+=vy;		}		if (vy>15){//limit falling speed			vy=15;		}									if (				helpmeghost.x >= path.x && 				helpmeghost.x <= path.width && 				helpmeghost.y >= path.height &&				helpmeghost.y >= path.y)				{				helpmeghost.y=path.y; 		// set pos of ghost to platform/base/path				vy=0; 						// switch gravity off				jumping = false;				inAir = false;							}else{				inAir = true;			}					if(helpmeghost.x>=550){helpmeghost.x = 550}  //keep ghost on stage		if(helpmeghost.x<=60){helpmeghost.x =60}

You can see all the code (I wouldn't look at it unless I had to) and results here...



Link to comment
Share on other sites

Hi no not yet, it's all in progress at the moment.


Also the current version is shortened for testing - so not much game play - actually hang on a minute


There, lasts for longer now..


The eyeballs will grow on the various platforms and my ghost will have to squash them

before they explode. If they explode -1000 points. If they get squashed +1000 points.

It'll be a couple of days maybe. Happy dreams SEE you later.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...