Platform Jumping
In this tutorial you will learn how to make a jumping system that could be used to make a platformer game or any other game involving jumping.
You should have taken my Moving MovieClips with the arrow keys tutorial because I will not be explaining that code.
First of all make you ground/platforms for your character to jump off and onto. If you have 1, give it an instance name of ground0. If you have 2 give them instance names of ground0 and ground1. For 3 give them instance names of ground0, ground1, ground2. If you have more than 3 you should have figured out by now what instance names to give them, if not I suggest you stop taking this tutorial and start learning English
Now make the MC you want to jump and put this code on it: (Please read the comments so you understand what the code does.
var jump:Number = 0; //Sets a new variable called jump with a value of 0
var jumping:Boolean = false; //Sets a new boolean which will be false when it loads
var falling:Boolean = false; //Read above
var g:Number = 4; //This variable should be how many grounds you have. (If it goes up to ground2, then to should have 3 grounds. ground0, 1 and
}
onClipEvent(enterFrame){
if(jumping == true || falling == true){ //If it is jumping or falling (|| means or)
this._y -= jump; //The _y coordinates increase by the variable jump
}
if(jumping == false){ //If jumping is false
if(!falling){ //If falling is false (! mean not, so !falling means not falling)
jump = 0; //Jump equals 0
}
if(Key.isDown(Key.UP)){ //If the up arrow key is down…
jump = 15; //Jump equals 15 and…
jumping = true //Jumping is true
}
}
if(jumping == true){ //If jumping is true…
jump -= 1; //Jumping decrease by 1
}
if(jump <= -10){ //If jump is equal to or less than negative 10…
jump = -10; //Jumping equals -10. Then it will not go any lower than -10
}
for(i=0;i<g;i++){ //This is a for loop. It keeps doing the code in between the curly braces until the condition (i<g) is false. g is the variable that has the number of grounds.
if(jumping == true && this.hitTest(_root["ground"+i])){
/*If jumping is equal to true (jumping == true), and (&&) if this hits any of the grounds… (_root["ground"+i] will be _root.ground and then whatever i equals, so if i = 2, it would be _root.ground2. Since it is in a loop it will equal all the numbers to match all your grounds, if you get what I mean. That probably didn’t make make much sense.*/
jumping = false; //Jumping equals false
}
}
if(Key.isDown(Key.LEFT)){ //You should have taken my moving movieclips with the arrowkeys tutorial so I won’t need to explain this.
this._x -= 5;
}else if(Key.isDown(Key.RIGHT)){
this._x += 5;
}
for(i=0;i<g;i++){ //Another loop
if(!jumping && !this.hitTest(_root["ground"+i]) && !falling){ //If jumping is flase and this isn’t hitting any of the grounds and falling is false…
falling = true; //Falling is true and…
this._y -= 1; //This _y decreases by 1 so it doesn’t fall through.
}
}
if(falling == true){ //If falling is true
for(i=0;i<g;i++){ //Another loop
if(this.hitTest(_root["ground"+i])){ //if this hits any of the grounds…
falling = false; //falling is false
}
}
jump -= 1; //jump decreases by one
}
}
Please don’t just copy and paste that code, please read all the comments so you know what it means.















ive added this to my character and well theres a couple of errors and im not the bast at flash so i cant always figure it out so wat do you think:
**Error** Scene=Scene 1, layer=Layer 1, frame=4:Line 63: ‘)’ expected
if(jump $lt;= -10){ //If jump is equal to or less than negative 10…
**Error** Scene=Scene 1, layer=Layer 1, frame=4:Line 66: ‘;’ expected
for(i=0;i
Total ActionScript Errors: 2 Reported Errors: 2
I don’t reply to this comment but i didn’t find the new comment button or such thing.
Can’t you make the comments so shorter? It’s WAY to long! And i can’t read it cause the letters is too small! Fix this please. Anyway your other tutorials are great!
dont worry ive figured it out thanks for the tutorial and may i ask if you could make one on A.I. i would praise you if you did thx
Yea, I will make one soon. Maybe today.
Also you must have got the code the same second I posted it because I made a few mistakes and I fixed it pretty much instantly so the code should be right now.
awesty you have so much extra code for the ground and all i want to know how to do is jump…help me plz
thanks for the tutorials.
they’re great
but on this one when ever I test it my man just appears in the middle and falls below the screen
any ideas?
thanks
Well you have to put a platform below it lol.
yea i did awesty it works pretty good now thx im looking forward for the AI one
hbradley i had the same problem with the falling through but it turned out that i forgot to name the ground movieclip ground0 that maybe the problem but im no expert.
thanks joey
I’ll check and see
Okay, I am going to try and write two new tutorials tonight. The AI one will be the second so that might not get finished.
can someone put what im suppose to put without explaining parts, ive read its just its getting annoying taking it out :p
webber are you a complete idiot take it out yourself dont be a lazy bitch
y are you so lazy do it your self
bcz i did and it didnt work so i screwed something up and i dont understand the error box
I did that purposely so people like you couldn’t just copy and paste it in. If you can really understand the code (which you should once you read all the comments) it should be simple to fix it.
i did but i didnt understand
Okay, comments are marked with a //. That way flash knows it’s a comment and not code, so anything after a // that doesn’t look like code you can remove. Multi-line comments a marked with a /* and closed with a */ so anything between them you can delete to
and ofcourse the platforms should be MC?
awesty ure tutorials are quite hard to follow, the health system thing neva worked for me, and now neither is this
none of ure comments start with /* so its quite hard to tell if theyre multiline or not
MC = Movieclip
Anything that follows // and doesn’t look like code get rid of. I can’t make it any clearer.
I understand all the comments because I do a lot of programming with other languages, but I’m not so experienced with Actionscript. I keep getting the following error and have tried to fix it multiple times, but I hasn’t been working. It might be because I’m using Flash MX 2004…
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 36: Operator ‘&&’ must be followed by an operand
if(jumping == true && this.hitTest(_root[”ground” i]==true)){
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 47: Operator ‘!’ must be followed by an operand
if(!jumping && !this.hitTest(_root[”ground” i]) && !falling){
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 53: Syntax error.
if(this.hitTest(_root[”ground” i])){
Total ActionScript Errors: 3 Reported Errors: 3
Actually, on the third line of the error I just posted, I should have entered it this way:
if(jumping == true && this.hitTest(_root[”ground” i])){
I added the “==true” when I was trying to fix it…
That is weird, the code works fine for me. It should work fine with flash MX 2004. Are you are it is 2004, if not that would explain the problem.
Awesty — thank you much for your tutorials. I’ve started going through them all tonight and have a super basic interactive sort-of game created already. I like your short-and-sweet approach (I’m a developer so it’s just the pace I need), but I hate the way it’s all formatted.
Your indentations are lost in HTML making the code kinda hard to read unless I view source, but then I have to trim out all the ’s and replace all the & with &.
Maybe you can use tags around your code samples?
Er my comment got botched. The last line should’ve ready “Maybe you can use <pre> tags around your code samples
Yea, i did but then the comments ran to long and off the page
Is this for flash 8?
i doesnt work for me. i just got flash and i dont know a thing. HELP PLEEZE!
wow… i never thought there could be a tutorial so great! thanx! gave me good stuff in my “maze” game!
what comes after ground4?
@Joe: Yes, it is for flash 8. I cant really help you if I don’t know exactly whats wrong.
@Unknown: No problem.
@Moo moo: ground7
how do u make a menu and a button that will activate the flash game?
http://www.awestyproductions.c.....-in-flash/
okay to put it simple this does not work at all there are to many errors and frankly my guy doesnt move AND YES I DID PUT THE MOVEMENT ON HIM FROM UR LAST TOT
Well that doesn’t really help me to help you.
Just calm down and send the .fla to admin[@]awestyproductions[.]com.
lol sry i was kidding i just couldnt ghet it 2 work but its ok i was just kidding
but just incase u wanted to no there is a Jumping thing on Flashkit.com and it has less action script errors and it usually works except for 1 thing maybe you could hunt it down and modify it. (it will let u download the .fla file :))
my guy just keeps falling through the blocks
but there aren’t any error messages
please help!
Umm Awesty i didn’t copy and paste this, i have 9 errors ._. plz help
**Error** Line 1: Clip events are permitted only for movie clip instances
onClipEvent(load){
**Error** Line 6: Clip events are permitted only for movie clip instances
onClipEvent(enterFrame){
**Error** Line 26: This type of quotation mark is not allowed in ActionScript. Please change it to a standard (straight) double quote.
if(jumping == true && this.hitTest(_root[”ground” i])){
**Error** Line 36: This type of quotation mark is not allowed in ActionScript. Please change it to a standard (straight) double quote.
if(!jumping && !this.hitTest(_root[”ground” i]) && !falling){
**Error** Line 42: This type of quotation mark is not allowed in ActionScript. Please change it to a standard (straight) double quote.
if(this.hitTest(_root[”ground” i])){
**Error** Line 6: Statement block must be terminated by ‘}’
onClipEvent(enterFrame){
**Error** Line 50: Syntax error.
**Error** Line 1: Statement block must be terminated by ‘}’
onClipEvent(load){
**Error** Line 50: Syntax error.
@RedV: Did you get all the instance names on the right movieclips?
@Bryce K: It looks like you have out the script on a frame instead of a movieclip.
My character sinks in to the gournd slightly when he hits it. How can i stop thi=s from happening?
Also can u make a tutorial for scrolling backround please!!!
awsety i made the changes but i still have 2 errors
**Error** Scene=Scene 1, layer=Layer 1, frame=15:Line 26: Statement block must be terminated by ‘}’
onClipEvent(load){
**Error** Scene=Scene 1, layer=Layer 1, frame=15:Line 71: Syntax error.
}
dangit i cant go any farther lol (sry i took up so much space ill e-mail my script to you)
@Adman: Try changing it to this:
onClipEvent(load){
var jump:Number = 0; //Sets a new variable called jump with a value of 0
var jumping:Boolean = false; //Sets a new boolean which will be false when it loads
var falling:Boolean = false; //Read above
var g:Number = 4; //This variable should be how many grounds you have. (If it goes up to ground2, then to should have 3 grounds. ground0, 1 and
}
onClipEvent(enterFrame){
//if(jumping == true || falling == true){ //If it is jumping or falling (|| means or)
this._y -= jump; //The _y coordinates increase by the variable jump
//}
if(jumping == false){ //If jumping is false
if(!falling){ //If falling is false (! mean not, so !falling means not falling)
jump = 0; //Jump equals 0
}
if(Key.isDown(Key.UP)){ //If the up arrow key is down…
jump = 15; //Jump equals 15 and…
jumping = true //Jumping is true
}
}
if(jumping == true && this.hitTest(_root[”ground” i])){
if(!jumping && !this.hitTest(_root[”ground” i]) && !falling){ //If jumping is flase and this isn’t hitting any of the grounds and falling is false…
if(this.hitTest(_root[”ground” i])){ //if this hits any of the grounds…
if(jumping == true){ //If jumping is true…
jump -= 1; //Jumping decrease by 1
}
if(jump <= -10){ //If jump is equal to or less than negative 10…
jump = -10; //Jumping equals -10. Then it will not go any lower than -10
}
for(i=0;i
/*If jumping is equal to true (jumping == true), and (&&) if this hits any of the grounds… (_root[”ground” i] will be _root.ground and then whatever i equals, so if i = 2, it would be _root.ground2. Since it is in a loop it will equal all the numbers to match all your grounds, if you get what I mean. That probably didn’t make make much sense.*/
jumping = false; //Jumping equals false
}
}
if(Key.isDown(Key.LEFT)){ //You should have taken my moving movieclips with the arrowkeys tutorial so I won’t need to explain this.
this._x -= 5;
}else if(Key.isDown(Key.RIGHT)){
this._x = 5;
}
for(i=0;i
falling = true; //Falling is true and…
this._y -= 2; //This _y decreases by 1 so it doesn’t fall through.
}
}
if(falling == true){ //If falling is true
for(i=0;i
falling = false; //falling is false
}
}
jump -= 1; //jump decreases by one
}
}
Sorry about all the comments but I couldn\’t really be bothered taking them out.
@Bryce K: I got your email and deleted your 2 other posts since they took up a far bit of scrolling space.
I will try and fix it when I have time.
This tutorial is great exept i got 1 error :
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 7: Statement block must be terminated by ‘}’
onClipEvent(enterFrame){
Please help. Thank you.
Just add one of theses, }, to the end of your code. It should work.
ok i did that but now my guy just falls and when i press up he flies and doesnt stand on the platforms, can i send you the .fla file?
Yep. My email it admin[@]awestyproductions[.]com.
hey when I try to jump onto the platform, i jump and go right through it
please help!?!?!?!
tyler
if u dont know… i wanna be able to stand on the platform
P.S. is there any way to make the enemys attack you?
I tried putting “}” and “{” these everywhere but i still have these errors
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 6: Statement block must be terminated by ‘}’
onClipEvent(enterFrame){
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 49: Syntax error.
PEOPLES IF UR GUY FALLS THERW THE FREAKIN WALL IT MEANS YOU DIDNT PUT AN INSTANCE NAME ON THE GROUND name it ground0. (Without the period)
ok i did everything with instance names and the code
and speaking of the code i put it on the guy and i still have 3 errors please help and replace them…
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 28: This type of quotation mark is not allowed in ActionScript. Please change it to a standard (straight) double quote.
if (jumping == true && this.hitTest(_root[”ground” i])){
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 39: This type of quotation mark is not allowed in ActionScript. Please change it to a standard (straight) double quote.
if(!jumping && !this.hitTest(_root[”ground” i]) && !falling){ //If jumping is flase and this isn’t hitting any of the grounds and falling is false…
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 46: This type of quotation mark is not allowed in ActionScript. Please change it to a standard (straight) double quote.
if(this.hitTest(_root[”ground” i])){ //if this hits any of the grounds…
Total ActionScript Errors: 3 Reported Errors: 3
@Both of you: Just email the .fla’s to me. It is much easier for me to understand what is going on.
admin[@]awestyproductions[.]com
Wow, what a great tut awesty! Thanks a lot. But I have one problem… my character bumps like like on each platform and when I jump and the character lands, he falls a little bit trough the platform and then he rises again. After that he starts bumping again.
Could you please help me?
Keep up the good work your tuts are great!
it wont let me send it to u
I send mine to you a 10 days ago. Are you going to email back or are you going to post a comment?
@BeM: Try changing the number on the last line (something like jump -= 1;) to a smaller number, is will still bump, but not as much.
@Tyler: I cant help you there.
@Daniel: Are you sure I haven\’t replied? Otherwise I mustnt have got the email.
Thanks awesty, but stille my character is rising threw the platforms. For example when I jump underneath a platform and it hits a platform it starts rising up till it’s on top of that platform…
Can I do something about that?
Hey awesty,
i tried your tutorial for platform jumping buti got 12 errors. Using a basic knowledge of actionscripting i was able to understand your code and deleted the “//”s and the comments but i get the following three errors.
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 27: Operator ‘&&’ must be followed by an operand
if(jumping == true && this.hitTest(_root[”ground” i])){
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 38: Operator ‘!’ must be followed by an operand
if(!jumping && !this.hitTest(_root[”ground” i]) && !falling){
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 45: Syntax error.
if(this.hitTest(_root[”ground” i])){
Total ActionScript Errors: 3 Reported Errors: 3
I am sending you the flash file document currently.
Please help. Thank you.
Mohit
You need to change the ” around ground. When you do it properly it turns into green.
@BeM: Try taking out the line:
jump -= 1; //jump decreases by one
@Mohit & BeM: Yea, I think its because this site as a different font, when you paste it flash cant read the quotation marks properly.
yah awesty i e-mailed u like 3 weeks ago >.> and i never got a response…
Hmm… I can remember emailing you back. Thats weird. I have the message in my sent box.
o.. could u post it cause i guess my e-mail is screwed up
onClipEvent(load){
fight = false;
}
onClipEvent(enterFrame){
if(fight == false){
if(Key.isDown(Key.LEFT) && fight != true){
this._x -= 4;
this._xscale = -100;
this.gotoAndStop(2);
}else if(Key.isDown(Key.RIGHT) && fight != true){
this._x = 4;
this._xscale = 100;
this.gotoAndStop(2);
}else{
this.gotoAndStop(1);
}
}
if(Key.isDown(Key.SPACE)){
this.gotoAndStop(3);
fight = true;
}
if(this._currentframe == 1){
fight = false;
}
}
onClipEvent(load){
var jump:Number = 0;
var jumping:Boolean = false;
var falling:Boolean = false;
var g:Number = 4;
}
onClipEvent(enterFrame){
this._y -= jump;
if(jumping == false){
if(!falling){
jump = 0;
}
if(Key.isDown(Key.UP)){
jump = 15;
jumping = true
}
}
if(jumping == true && this.hitTest(_root[”ground” i])){
if(jumping == true){
jump -= 1;
}
if(jump <= -10){
jump = -10;
}
for(i=0;i
}
if(!jumping && !this.hitTest(_root[”ground” i]) && !falling){
if(this.hitTest(_root[”ground” i])){
}
if(Key.isDown(Key.LEFT)){
this._x -= 5;
}else if(Key.isDown(Key.RIGHT)){
this._x = 5;
}
for(i=0;i
falling = true;
this._y -= 1;
}
if(falling == true){
for(i=0;i
falling = false;
}
}
jump -= 1;
}
}
}
Thanks for the help but that doesn’t work either it keeps saying all clip events have to appear on a clip handler when they are attached to a movieclip anyway could you check out my version of the game its rite here, the only thing wrong with is is you go through the ground even is the code is on it…
[url=http://www.swfup.com/swf-view.php?id=3509]View my swf file![/url]
wow, thats weird i just played my game again… and i didnt fall through but on my comp. i do… cool
anyway if you wanna see the code here it is
(i just give the ground an instance name of ground)
onClipEvent (load) {
moveSpeed = 10;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
this._x = moveSpeed;
}else if (Key.isDown(Key.LEFT)) {
this._x -= moveSpeed;
}
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.UP) && !jumping) {
vel_y = 36;
jumping = true;
}
if (jumping == true) {
vel_y -= 2;
if (vel_y
awww dammit i hate when it gets cut off… I’ll e-mail it to u so you can make it better, if you want
It won’t get cut off if you take out the angle brackets (< and >)
It works fine on my computer to, maybe its the version of flash on that computer.
yes i finally got it to work Thnaks for the tutorial but, my guy keeps falling through the ground XD and i have all my instance names -_-
You can email it to:
awestyproductions[@]gmail[.]com
If you want. I will try and fix it.
Thanx man it looks sick now
No problem.
I need help please? how can i make my char jump when i press up?
thnx awesty
Ummm, read the tutorial.
i did but it wont work…
im about to try the platforms but theres one prob. Im gona have the platforms moving sideways in a motion tween so u hav to try and catch it, and i cant make it in a loop or it will go back to the the first frame where the health, etc, is set back to its original varible etc. Is ther anyway just to loop the tween?
@KIM: Read it again, I just changed it a bit.
@RBD2006: Make the platform a MC, and tween it inside the MC. So on the main timeline it is only 1 frame.
ty awesty this is one of the best tutz
therz just one more thing. Is it possible to make it that if you fall of a platform, you fall, but down the bottom of the screen and a varible for your life decreases? Or something like that to indicate you lost a life
its not very accurate, it will jump onto platforms, but with platforms that are close, if you walk off one it goes up onto the other one. Maybe cuz only the surface line should be my mc, im not sure but i thought id ask
byrce, i had the same problem if u havent fixed it already, make the platform a 0.25 line, and call it ground0 and just move it ontop of the thing u want to jump onto, i had the same prob cuz i was making a function wer u jump onto a tree, and accidently made it all a platform etc
hey he jumps to high, can anybody help or highlight the code that tells him how high he jumps?
@RDB2006 - thanks for the advice.
@RBD2006: Make a MC off the screen, and put a code like this on it:
onClipEvent(enterFrame){
if(this.hitTest(_root.man){
_root.life -= 1;
}
}
And have a dyanamic text for the health with a variable name of life.
@Bobkins: This:
jump -= 1;
And:
jump = 15;