00:00
00:00
Newgrounds Background Image Theme

wilwz just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS: my jumping engine

4,984 Views | 15 Replies
New Topic Respond to this Topic

AS: my jumping engine 2006-05-15 10:43:07


AS: Main

there have been a few people asking how i make my jumping engines, so i forced myself to write this tutorial.it is also my first.

at the end of the tutorial, you will get something like this: My Jumping Engine
note that the .swf is not a complete game, i just made it for tutorial purposes.

s to jump,
left and right to move.
the red things are enemies (note: i won't be teaching how to make enemies just yet)

if you don't like how the engine looks, or think that i am in no position to make a tutorial because i am a noob, or for whatever reason, then don't bother even reading this, you hear?

first of all, let me explain that i am not going to put exact code, just pseudocode, so you just can't copy and paste, and that i will put code of different use in different onClipEvents to make it easier to understand, so experts, stop saying stuff like "Put that in one onClipEvent!", this is made for beginners, anyway.

so, your first step is to animate your character., fame 1 being the standing frame. add walking left and right animations, and a jumping animation.
then put stuff like this on the character MC named "player":

onClipEvent(enterframe){
if(Key.isDown(Key.RIGHT)&&this._currentfra
me==1){
gotoAndPlay(the frame where he faces right)
}
else if(Key.isDown(Key.LEFT)&this._currentframe
==1){
gotoAndPlay(the frame where he faces left)
}}

if you're wondering why there is a "&&this._currentframe==1", this is to make sure it doesn't stop at the frame you made it gotoAndPlay to.do this only if your walking animations are directly inside the player MC, not when you have "walking left" and "walking right" MCs inside the player MC. otherwise, leave that code out.

gravity:
declare these variables first:

onClipEvent(load){
_root.gracvity=10
_root.speed=10
}

kep in mind that the value of these variables is up to you, tweak the values if you see that they are unsuitable for your view of a game.

put this in your character MC:

onClipEvent(enterframe){
if(!(_root.bkg.walls.hittest(this._x, this._y, true)or_root.bkg.walls.hittest(this._x+(th
is._width/2-5), this._y, true)or_root.bkg.walls.hittest(this._x-(th
is._width/2-5), this._y, true))&&this._currentframe<=(the frame that shows the jumping animation))){
//this means if the character is in the air, or jumping
this._y+=_root.gravity
_root.gravity+=0.5
//this is to give the illusion of increasing fall speed, even though it is just tiny

}
else{
//this means if the character is on land
_root.gravity=10
//gravity reverts to its normal value
}}

jumping:

animate your character jumping, and on each frame where he is supposedly going vertically, add this code in the frame:

_root.player._y-=(_root.gravity+10)

this is so that you know exactly what value your character can jump.
for example, if there are 6 jumping frames, the first two can have a net height increase of 15, the next two 10, then the last two 5, to give the illusion of it slowing down.this is the part with which most ASers disagree with.then again, i said it's my jumping engine, so that means i never ripped from anyone.it was my idea entirely.

then put this code in your MC:

if(Key.isDown(Key.UP)&&this._currentframe<
=(the frame that shows the jumping animation)){
gotoAndPlay(the frame that has the jumping animation)}

REMINDER: i can almost feel you saying to your computer screen "this code is st00peed!what kinda noob made this?!" well, as i said, this is for a jumping engine that will look like the .swf in the link above.i'm just telling you how it is made.if you don't like it so far, PLEASE STOP READING

walking:
put this in your character MC:
if(Key.isDown(Key.RIGHT)&&!_root.bkg.walls
.hittest(this._x+(this._width/2-5), this._y-(this._height/2-5), true)&&this._x<(insert width of movie here, subtract 100 from it. this is so, the character can only walk a certain distance from the center)){
this._x+=_root.speed
else if(Key.isDown(Key.RIGHT)&&!_root.bkg.walls
.hittest(this._x+(this._width/2-5), this._y-(this._height/2-5), true)&&this._x>=(insert width of movie here, subtract 100 from it.)){
_root.bkg._x-=_root.speed
}

do this for when pressing left too, i guess you can do that on your own.

the effect is like the one in metal slug, where the background moves only if the character is near the edge of the screen.

this works better in 24 fps, by the way.
make an MC "wall" inside an MC "bkg", then in the "bkg" MC, on a layer above the "wall" MC, draw the level in detail. then make the alpha of the "walls" MC be 0 to make it invisible.

and there you have it, that is the engine i work with.cheerio!

REMINDER: this tutorial is to show you how i do it, this does not necessarily (sp?) mean it is the only and the best way to do it. for more fun and learning, make your own engine, i don't want people flooding the portal with half-baked platformers that look all alike.

Response to AS: my jumping engine 2006-05-15 10:45:42


nice one! i can't believe that a filipino has joined the As:Main!

Response to AS: my jumping engine 2006-05-15 11:00:34


It's alright, but the jumping needs gravity. The jumping upwards looks alright, but whens its falling, make the speed that it falls at increase.

Response to AS: my jumping engine 2006-05-15 11:16:18


At 5/15/06 10:45 AM, Chanmanster wrote: nice one! i can't believe that a filipino has joined the As:Main!

kababayan!
nice to know i'm not alone, and thanks!

Response to AS: my jumping engine 2006-05-15 11:53:42


The game in the link you posted doesn't work. I pressed up and nothing happened. :S Good work though.


BBS Signature

Response to AS: my jumping engine 2006-05-15 12:06:50


At 5/15/06 11:53 AM, -Toast- wrote: The game in the link you posted doesn't work. I pressed up and nothing happened. :S Good work though.

umm, it says there, "s" to jump.
i did the .swf and tutorial separately(sp?).
sorry, and thanks.

Response to AS: my jumping engine 2006-05-15 13:10:58


That one was nice, it's got the same feel to it as Alien hominid :D I like that ;)

Response to AS: my jumping engine 2006-05-15 13:23:22


Yeah, I agree, good work. I tried out the short game in your link and it runes pretty smoothly


The Juggernauts will awaken...

Response to AS: my jumping engine 2006-05-15 13:58:21


At 5/15/06 01:10 PM, seel wrote: That one was nice, it's got the same feel to it as Alien hominid :D I like that ;)

wow, being compared to Alien Hominid!
that comment made me happy!
thanks!

Response to AS: my jumping engine 2006-07-28 04:04:57


Nice work dude. I didn't notice you're Pinoy until I saw the poster below you.
"D ko akalain na may mga ibang pinoy rin dito. Well, salamat pre. Ganda ng pagkakagawa mo."

And, the demo game was also great. It has a nice feel into it.

Response to AS: my jumping engine 2006-07-28 05:58:11


like cybex needs, it needs gravity

gravity ACCELERATES bodys, i.e. it increases acceleration linearlly, increasing velocity exponentially

simplest method is to define a new variable, vy or something

then in the movement section of code, you have player._y += vy;

when you jump, you set vy to some negative number, and while in the air, you add a constant value to vy (maybe 0.6) and it will cause it accelerate downwards in a realistic way, once hit the ground, you set vy to 0

Response to AS: my jumping engine 2006-07-28 07:07:51


Oi, mga kababayan! Medyo marami rami rin pala tayong mga Pinoy dito, no? Start tayo ng thread para sa pinoy sa general discussion.

Response to AS: my jumping engine 2007-03-12 23:17:17


ok well i tried it im pretty sure i got all the errors in it but its not working right for me at all.... like the character just drops really slowly if i hit right he stop falling as long as im holding it... none of the ground colusion works .. can u help?

Response to AS: my jumping engine 2007-03-12 23:53:32


bad idea to animate the jump. really primitive. overall nothing special. but still helps to learn this. good tut

Response to AS: my jumping engine 2011-07-29 02:57:50


one little snippet i made:

if(grav < 0)&&!_root.ground.hitTest(_x,_y,true)){//change grav to your gravity variable
this.gotoAndStop(4); //change 4 to whatever frame the falling animation is
}

it's used to play a falling animation.
it's untested so it might go wrong somehow.