• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

# Help with jumping

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

44 replies to this topic

### #21fastcall22  Moderators

Posted 13 October 2011 - 09:35 PM

Have you tried Visual Studio?
zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

### #22DaveMS  Members

Posted 14 October 2011 - 01:34 AM

Your going to have to help us to help you - your not making it easy for the people trying to help you. Rather than posting your code in a zip file, post it directly on the forum, most people are not going to be bothered to download and compile your source - they will just move onto the next post.

Second, you need to tell us why it isn't working. give us a walk through of what you think should be happening, then tell us what is actually happening. give us error messages.

Third, programming isn't about writing code - that is the end product, the job of a programmer is problem solving. Tell me in english how you would make the character jump. If you can't do that, then your not going to be able to tell the computer how to do it in code.

### #23JustinDaniel  Members

Posted 14 October 2011 - 01:50 AM

Theres one thing i have to say you.
In programming you dont learn by asking other, Make mistakes and learn from your own mistakes that's the way to learn programming, i can see a junk of code being copy pasted in your code. I just gave you an idea on how jumping works you gotta learn from it, not just blindly copy paste.
Anyhow for your code.
Lets go to your "player.h" file
#ifndef PLAYER_H
#define PLAYER_H

#include "timer.h"

//The dimmensions of the player
const int PLAYER_WIDTH = 32;
const int PLAYER_HEIGHT = 32;

//The status of the player
//standing
const int PLAYER_STAND = 0;
//walking right/left
const int PLAYER_RIGHT = 1;
const int PLAYER_LEFT = 2;
//jumping
const int PLAYER_JUMP = 3;
const int PLAYER_FALL = 4;

//Telling whether or not to use animation for the player
int PLAYER_WALKING = false; //true if walking, false if everything else

//The player
class Player
{
private:
//The offset
int x_offSet;
int y_offSet;

//Its current frame
int frame;
Timer playerTimer;

//Its animation status
int status;
int vertical_status;

enum Vertical_States
{
V_JUMPING_UP,
V_APEX_REACHED,
V_FALLING,
V_GROUNDED
};

// jumping-related stuff for now
static const int gravity = 4;
int jump_apex;

int x_pos;						// horizontal position
int x_velocity;					// horizontal velocity

int y_pos;						// vertical position
int y_velocity;					// vertical velocity
int y_acceleration;
int Jumping;

int surface_height;				// last known good level to stand at

public:
//Initializes the variables
Player();

//Handles input
void handle_events();

//Moves the player
void move();

void updatePosition();

//Shows the player
void show();

//The jumping/falling/and ground with the player
void handleJumping();
void fall();
};

#endif


Thats what i call A Junk of unused codes in a class. Lets see,
You have a enum state in your class and you never use it.
static const int gravity = 4 ??
I dont see you using it anywhere
And what's
int jump_apex;

There are many more errors and void's which you never call from your class and they are useless. So try to create your class in sort of a decent way and just store variables which your going to use.
The main issue is, I dont get it why it doesnt work for you, Even i follow that method to produce a jumping state,
Anyways in Line 224 try taking away that break; statement, I dont know if that might work just try it
we all just gave you an idea what jumping is and its upto you we can't help you more or Just type the code for you, You will never learn if you dont learn from your mistakes.

### #24szecs  Members

Posted 14 October 2011 - 03:21 AM

Tell me in english how you would make the character jump. If you can't do that, then your not going to be able to tell the computer how to do it in code.

I just wanted to quote this for emphasis. If you can clearly explain the behavior what you want in detail, then you almost only have to translate that to the programming language of your choice (translation means simply mapping the expressions to the proper syntax) Shit, I'm sure that doesn't mean what I wanted...

To the OP: if you want code, and you basically want someone else to do the work (writing code is jack shit itself, debugging and problem solving is the real work), then you'll have to pay for it. Either in nature or with hard cash.
People who asked for solutions in my university (the "do it for me" kind of request, like you do) either paid in cash or in beer, and no body liked them. They were the parasites, who eventually (after 7-8 years instead of the 5) got their degree, but went nowhere from there.

Asking for ready solutions can happen from time to time, anything can happen, life is just like that, but doing the same with your own hobby is a mystery for me. Like asking someone else to do the "hard work" on your girlfriend....

EDIT: maybe you are just on the wrong track. Wrong track = learning from others' code as a beginner. I'm sure someone can link to articles that carefully explain why this is not good (for a beginner). Learn by doing on your own. (Well, I'm against learning from tutarials too, but that's a different matter).

### #25Serapth  Members

Posted 14 October 2011 - 07:16 AM

then you'll have to pay for it. Either in nature or with hard cash.

So, like with pinecones, rocks and shit? ;)

EDIT: maybe you are just on the wrong track. Wrong track = learning from others' code as a beginner. I'm sure someone can link to articles that carefully explain why this is not good (for a beginner). Learn by doing on your own. (Well, I'm against learning from tutarials too, but that's a different matter).

I am somewhat against tutorials that just show how instead of why. However, I think in writing my own tutorial I hit on the right balance. I try to take the user through the actual process, so that at points the tutorial actually goes back an reworks previous code so the user actually understands *why* you do something. Plus, its half tutorial and half instruction. Granted, someone could jump to the last chapter, grab the sample code and run from there but that's their choice.

We all learn differently and we all need to learn from something. My beef is tutorials that simply say do this then that then this, which sadly is the majority. A person isn't really learning anything from this, other than brushing up their typing skills. Now the downside is, in order to explain the why with the how, the tutorial writer needs to spend a lot more effort and the tutorials tend to be much longer.

### #26DrNicholas  Members

Posted 14 October 2011 - 09:51 AM

Like ive said, I knew this was going to happen, I knew you werent just going to help me with code. You are are riddles and stuff. I learn by studying, you may learn differently, I don't. I don't have the personal time to invest in learning something complex, im sure you were the same (you learned in a classroom). You are all beyond that point that you are 1337.

Now asI said in my very first post on this thread, I would have liked down right help, but nope.avi, you just proxied away at the conversation. If you want to help me how I like to be helped (and stop assuming things differently, I dont want a game, I want a player), thats fine.

Thanks

### #27unbird  Members

Posted 14 October 2011 - 10:30 AM

Ok, printf debugging help ahead.

Dump the offsets right before the apply_surface call, like
  printf("x_offSet=%d,y_offSet=%d\n", x_offSet, y_offSet);


Then put another one within apply_surface dumping the parameters, like:
  printf("x=%d, y=%d\n", x, y);


Run your app and study the console output.

### #28BeerNutts  Members

Posted 14 October 2011 - 10:31 AM

Like ive said, I knew this was going to happen, I knew you werent just going to help me with code. You are are riddles and stuff. I learn by studying, you may learn differently, I don't. I don't have the personal time to invest in learning something complex, im sure you were the same (you learned in a classroom). You are all beyond that point that you are 1337.

Now asI said in my very first post on this thread, I would have liked down right help, but nope.avi, you just proxied away at the conversation. If you want to help me how I like to be helped (and stop assuming things differently, I dont want a game, I want a player), thats fine.

Thanks

Whelp, good luck having someone make you a player who jumps, because, if you can't do that, then you won't be able to program a game either.

If you don't know what wrong with:
apply_surface( x_offSet && y_offSet, SCREEN_HEIGHT - PLAYER_HEIGHT && SCREEN_WIDTH - PLAYER_WIDTH, player, screen, &clipsRight[ frame ] );

when the prototype for apply_surface is:
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL );


Then, go back to the basics.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #29Cornstalks  Members

Posted 14 October 2011 - 11:00 AM

POPULAR

Like ive said, I knew this was going to happen, I knew you werent just going to help me with code. You are are riddles and stuff. I learn by studying, you may learn differently, I don't.

Learning differently is fine. But being lazy is not. You haven't even tried to write any psuedocode. Here's the catch: programming and writing software are just fancy words for problem solving. Not all problems are the same. In programming, most problems are different. While they have similarities, there isn't "one solution to every problem." You need to learn how to do problem solving. That will take time and effort.

I don't have the personal time to invest in learning something complex

Then you don't have time to learn how to program. Programming is complex. It always has been and it always will be. If you don't have the time to learn how to do it, and you know you don't, you're wasting what little time you have.

im sure you were the same (you learned in a classroom).

I learned from the Internet and wonderful sites like GameDev.net. Not in a classroom, actually. In fact, I've learned very, very little about programming in any kind of formal or informal class.

Now asI said in my very first post on this thread, I would have liked down right help, but nope.avi, you just proxied away at the conversation.

I'm going to pretend like I understand what that sentence is supposed to mean... You ever heard of the proverb "Give a man a fish, feed him for a day; teach a man to fish, feed him for a lifetime?"

If you want to help me how I like to be helped (and stop assuming things differently, I dont want a game, I want a player), thats fine.

What we're trying to say is that if you can't get basic jumping down in your game, you won't get anywhere. You won't finish. You probably won't even really start. So rather than go through all the work of making a demo of how to jump, just so you can study the code and see "Ah, so that'show they made him jump," and then you realize you have no freaking clue what to do with that code now, and then you come back and say "Hai guyz, I need help making my character shoot, plz give me code." Then you come back again and say "Now I need help making my character swim, I need you to give me the code." Then you come back and say "Now I need you to give me code to make my character fly." You know what? By the time you get all the code samples you're asking for, we'll have made the dang game for you.

So I'm just going to try and tell you something:

That's not how it works.

You want code? Here:

double characterVelocityY;
double characterVelocityX;
double characterPosX;
double characterPosY;

int main()
{
while (true)
{
if (spacebar_pressed) characterVelocityY = 10;
if (left_key_pressed) characterVelocityX = -4;
if (right_key_pressed) characterVelocityX = 4;
if (characterVelocityY > 0) characterVelocityY -= 0.5;
if (characterPosY < 0) characterPosY = 0;
if (characterPosY < 0) characterVelocityY = 0;
characterPosX += characterVelocityX;
characterPosY += characterVelocityY;
print_out_to_console("Character position x: " + characterPositionX + "    Character position y: " + characterPositionY);
}

}

If you aren't even going to make an honest effort to understand what I just wrote and translate it into whatever APIs/language/whatever you're using, then we've all been fooled and you're actually just an epic troll who fooled us all.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

### #30Sharpe  Members

Posted 14 October 2011 - 12:20 PM

POPULAR

Let me just say that as a beginner who has been graced with generous amounts of help here on GameDev.net, please don't let this spoiled child "DrNicholas" and his horrible attitude and disgusting sense of entitlement ruin it for the rest of us who are truly grateful for this community's openness and kindness to new members.

I've seen communities wherein the members get battered down over the years by whiny, unappreciative moronic children like Nicholas. Members begin to think, "Why help this person? Look at the way the last one acted!" They slowly become more and more closed to new members until the moderators and the old guard are so spiteful and elitist they become worse than the trolls.

Beernutts, JustinDaniel, Cornstalks, Serapth and others who have offered advice, your skill and willingness to help is greatly appreciated -- if not by Nicholas, who is probably too young to realize his shameful behavior, then by many others for whom you have so graciously donated your time and efforts to help.

Thanks, guys!

-Sharpe
A 30-year-old interested in learning to program by making an old-school, simple, text-only or 2D dungeon fantasy video game.

• As of 9/28/2011, currently attempting to learn the basics of Python 2.7.
• As of 10/14/11, Dabbling in C#.
• As of 10/24/11, decisively surpassed my small knowledge of Python in C#.

### #31szecs  Members

Posted 14 October 2011 - 03:40 PM

EDIT: sigh
Dunno why I'm so upset of these spoiled brats, most newbies here on Gamedev have good attitude actually, but these few guys totally get me down

### #32JustinDaniel  Members

Posted 14 October 2011 - 09:50 PM

Well, i should not be doing this, but uh, it makes me mad how people learn to program games when they dont even know basics of c++ or whatever language it might be, copying and pasting code doesnt get you anywhere, if you have to become a game programmer then you gotta work for it. Asking for help is not bad but asking others to write code for you is totally annoying especially in programming.
#include <sdl.h>
#include <iostream>
#include <sdl_image.h>
#include <string>
#include <cstdlib>
#include <ctime>

int main( int argc, char* argv[] )
{
std::srand( std::time(0) );

if( SDL_Init( SDL_INIT_VIDEO ) == -1 )
{
std::cout << "Failed  to initialize" << SDL_GetError() << "\n";
return false;
}
std::atexit(&SDL_Quit);

SDL_Surface *screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
if( screen == NULL )
{
std::cout << " Failed to set video " << SDL_GetError() << "\n";
return false;
}

SDL_Surface *square = IMG_Load( "square.bmp" );
if( square == NULL )
{
std::cout << "Failed to open square" << SDL_GetError() << "\n";
return false;
}

int x = 20;
int y = 460;
int vx = 0;
int vy = 20;
int ay = 2;
bool jumping = false;
bool quit = false;
while( !quit )
{
SDL_Event event;
while( SDL_PollEvent( &event ) )
{
if( event.type == SDL_KEYDOWN )
{
switch( event.key.keysym.sym )
{
case SDLK_RIGHT: vx += square->w /3; break;
case SDLK_LEFT: vx -= square->w/3; break;
case SDLK_SPACE:
vy = 20;
jumping = true;
}

}
else if( event.type == SDL_KEYUP )
{
switch( event.key.keysym.sym )
{
case SDLK_RIGHT: vx -= square->w /3; break;
case SDLK_LEFT: vx += square->w /3; break;
if( jumping )
{
if( vy >= -20 )
{
vy = vy - ay;
y = y + vy;
}
}
if( y + square->h > screen->h )
{
y = 460;
}
}
}
if( event.type == SDL_QUIT)
{
quit = true;
}
}
SDL_Rect rect = {x, y, square->w, square->h };

x += vx;

if( jumping )
{
y -= vy;
}
if( vy >= -20 )
{
vy = vy - ay;
}
if( y + square->h > screen->h )
{
y = 460;
}
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF,0xFF,0xFF ) );
SDL_BlitSurface( square, 0, screen, &rect );

SDL_Flip(screen);
}
SDL_FreeSurface( square );
return 0;
}


I have given you the entire code on how to make a square jump, Learn from it and see what you can do it with your game, Atleast go through some of the code and learn what it does, learn from it, DONT copy paste.
Peace.

### #33BeerNutts  Members

Posted 15 October 2011 - 05:42 PM

Justin, your code still isn't quite right. What happens when u hold down space?

Also, if holding left or right, you're going to apply double the gravity (ay) twice in one frame.

Jump velocity should be negative when 1st hitting space (moving up screen).

And, finally, as you hold down LEFT )or RIGHT) you're going to constantly increase the velocity. So, just set vx= (whatever), not vx += or vx -= when checking keys.

As I said in my 1st post, just check for space on key up, and only apply gravity when moving. Just set the velocity when checking key presses, don't apply it to x or y then.

Well, i should not be doing this, but uh, it makes me mad how people learn to program games when they dont even know basics of c++ or whatever language it might be, copying and pasting code doesnt get you anywhere, if you have to become a game programmer then you gotta work for it. Asking for help is not bad but asking others to write code for you is totally annoying especially in programming.

#include <sdl.h>
#include <iostream>
#include <sdl_image.h>
#include <string>
#include <cstdlib>
#include <ctime>

int main( int argc, char* argv[] )
{
std::srand( std::time(0) );

if( SDL_Init( SDL_INIT_VIDEO ) == -1 )
{
std::cout << "Failed  to initialize" << SDL_GetError() << "\n";
return false;
}
std::atexit(&SDL_Quit);

SDL_Surface *screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
if( screen == NULL )
{
std::cout << " Failed to set video " << SDL_GetError() << "\n";
return false;
}

SDL_Surface *square = IMG_Load( "square.bmp" );
if( square == NULL )
{
std::cout << "Failed to open square" << SDL_GetError() << "\n";
return false;
}

int x = 20;
int y = 460;
int vx = 0;
int vy = 20;
int ay = 2;
bool jumping = false;
bool quit = false;
while( !quit )
{
SDL_Event event;
while( SDL_PollEvent( &event ) )
{
if( event.type == SDL_KEYDOWN )
{
switch( event.key.keysym.sym )
{
case SDLK_RIGHT: vx += square->w /3; break;
case SDLK_LEFT: vx -= square->w/3; break;
case SDLK_SPACE:
vy = 20;
jumping = true;
}

}
else if( event.type == SDL_KEYUP )
{
switch( event.key.keysym.sym )
{
case SDLK_RIGHT: vx -= square->w /3; break;
case SDLK_LEFT: vx += square->w /3; break;
if( jumping )
{
if( vy >= -20 )
{
vy = vy - ay;
y = y + vy;
}
}
if( y + square->h > screen->h )
{
y = 460;
}
}
}
if( event.type == SDL_QUIT)
{
quit = true;
}
}
SDL_Rect rect = {x, y, square->w, square->h };

x += vx;

if( jumping )
{
y -= vy;
}
if( vy >= -20 )
{
vy = vy - ay;
}
if( y + square->h > screen->h )
{
y = 460;
}
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF,0xFF,0xFF ) );
SDL_BlitSurface( square, 0, screen, &rect );

SDL_Flip(screen);
}
SDL_FreeSurface( square );
return 0;
}


I have given you the entire code on how to make a square jump, Learn from it and see what you can do it with your game, Atleast go through some of the code and learn what it does, learn from it, DONT copy paste.
Peace.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #34Cornstalks  Members

Posted 15 October 2011 - 06:02 PM

As I said in my 1st post, just check for space on key up, and only apply gravity when moving.

While that's a totally valid solution, usually you want to implement character actions on key down events. The reason is because that's how 99% of games do it (so they'll expect it), and especially because there is a small delay between when a key is pressed down and when it is released up. Most gamers will be upset by the delay, expecting the game to respond as soon as they hit the key down.

It can work just fine in some games, but it's something to consider. Most games only allow jumping when the character is touching the ground. That way the character can't double jump, plus it can be implemented as a key down event without worry of double-processing the event.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

### #35MaxFire  Members

Posted 15 October 2011 - 07:17 PM

this thread was quite funny to read, its the common case of someone doing a few online tutorial believing they instantly know how to program and that they know best ignoring other peoples advice

### #36BeerNutts  Members

Posted 15 October 2011 - 07:27 PM

As I said in my 1st post, just check for space on key up, and only apply gravity when moving.

While that's a totally valid solution, usually you want to implement character actions on key down events. The reason is because that's how 99% of games do it (so they'll expect it), and especially because there is a small delay between when a key is pressed down and when it is released up. Most gamers will be upset by the delay, expecting the game to respond as soon as they hit the key down.

It can work just fine in some games, but it's something to consider. Most games only allow jumping when the character is touching the ground. That way the character can't double jump, plus it can be implemented as a key down event without worry of double-processing the event.

Well, you can do it, but the way Justin did it isn't valid. He didn't check if (!Jumping) first, which would be the way to do it. After thinking about it, it's better to do it on down press, and check if (!Jumping).

Good point.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #37DrNicholas  Members

Posted 19 October 2011 - 06:29 PM

### #38Dancin_Fool  Members

Posted 19 October 2011 - 06:38 PM

???

### #39Khaiy  Members

Posted 19 October 2011 - 07:17 PM

You've been given lots of code help and conceptual help, from people who have successfully coded up the behavior you're looking for on their own. At this point, I think it would be safe to say that the problem is on your end, not with the code samples you've been given. If you don't understand the help that has been provided, you're simply not ready to program something like this yet.

Posting Riddler pictures gets across that you don't understand, but you don't even appear to want to understand what's going on. Your posts in this thread have are all "Write the code for me so I can understand", and then when people have written the code for you, rather than working to understand it or asking for help in understanding it you just say "It doesn't work, thanks for nothing. Do it again". And even when people do that , or just ask you for more information so that they can help you better, you complain and say that that's not how you like to be helped. Oh, so sorry sir, please grace us with another opportunity to do it for you, only better.

If you want to learn how to program games or anything else, start figuring out how you could test your code to see why what you thought would work isn't working. That's programming, that's exactly it. Programming isn't taking pre-written code and then slapping new values in with no understanding of what in that code allows you to do so. Nor is it having others code your game for you and then "studying" it (i.e. having a working game feature and then not caring about how or why it works because you don't need to, since it works). If that's really the extent of what you want to do, then pick up one of the many game maker software packages out there-- they seem like they may be more your speed. You can still make cool games and do lots of scripting and have fun doing it.

But don't come in insisting that you want to learn how to draw when all you really want to do is color inside the lines someone else has drawn, and then make whining demands that can't be satisfied when it turns out that what you said you wanted isn't what you're actually trying to do.

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~

### #40Cornstalks  Members

Posted 19 October 2011 - 08:22 PM

He's just a troll. I doubt he ever had any intention of putting forth any effort, and rather just wanted to come piss a few people off for the lolz. Let's please just let this thread die. RIP thread.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.