• Advertisement
Sign in to follow this  

how to make a 2D sprite jump?

This topic is 4773 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Helo everyone, I'm actually writing a simple platform game but I am encounting a serious problem!!! My jump Function is working!! Below is the function, can someone figure out what is the problem? Thank a lot. void jump(SPRITE *pSprite) { #define PLAYER_FRAMES 4 int i=0; POINT old = pSprite->position; int curr_y = pSprite->position.y - 50; while (i < curr_y) { i += 1; pSprite->position.y = i; SetSpritePosition(pSprite, pSprite->position.x, pSprite->position.y); SetSpriteState(pSprite, 0); } //SetSpritePosition(pSprite, old.x, old.y); }

Share this post


Link to post
Share on other sites
Advertisement
i think the problem is your while loop.
you have to work it out in your mind.

void jump(SPRITE *pSprite)
{
#define PLAYER_FRAMES 4
int i=0;
POINT old = pSprite->position;
int curr_y = pSprite->position.y - 50;

while (i < curr_y)
{
i += 1;
pSprite->position.y = i;
SetSpritePosition(pSprite, pSprite->position.x, pSprite->position.y);
SetSpriteState(pSprite, 0);
}

//SetSpritePosition(pSprite, old.x, old.y);

}


in the while loop you are updating the position but not updating what's on the screen. the loop doesn't finish until the sprite is back on the ground. try it without the loop and see what happens. mostly it will jump right off the screen, but at least you'll have a clue. post back if you need any help.

edit: also i think you want it to say pSprite->position.y += i. because at this point, the sprite might be starting at the very top of the screen. curr_y looks a little suspicious also.

if you don't mind could you post the code for SetSpritePosition()?

Share this post


Link to post
Share on other sites
You might try giving your sprite a velocity, instead of trying to set absolute values for y. I know this doesn't explain the problems with your original loop, but you really will find it simpler in the long run, and it allows such things as a sprite jumping and landing on something which is higher than it started.


void jump
{
sprite->velocity.y = jump_speed;
}

void call_this_every_frame()
{
if (sprite_is_not_on_floor)
{
sprite->velocity.y -= gravity_accel;
}
sprite->position.y += sprite->velocity.y;
}


This also doesn't work (after the first jump it will fall through the floor) but it should get you started and you can debug it from there on your own.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement