🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

jumping sprite

Started by
14 comments, last by SuperVGA 2 years, 10 months ago

well I have got my sprite to jump multiple times but it does not stop, I just want my sprite to jump once when I hit the space bar.

void jumpsprite()
{
	positionY = -0.05f*(x*x) + 20;
	x++;
	if (x >= 20.0f)
	{
		x = -20.0f;
	}
	move_sprite++;
	cout << positionY << endl;
}

void Loop(int value)
{
	jumpsprite();
	glutPostRedisplay();
	glutTimerFunc(500, Loop, 0);
}

void handleKeypress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
		break;
	case 32:
//		jumpsprite();
		Loop(0);
		break;
Advertisement

What's

glutTimerFunc(500, Loop, 0);

Doing in the function you're triggering when jumping? It's also called “Loop” and takes an int…

it is a timer function, it calls the jumpsprite function over and over again.

pbivens67 said:

it is a timer function, it calls the jumpsprite function over and over again.

pbivens67 said:

well I have got my sprite to jump multiple times but it does not stop, I just want my sprite to jump once when I hit the space bar.

So it's intentional, but it surprises you at the same time? I'm not sure I really understand what the problem is then; the player presses a key and your animation will just loop. That's the idea?

you are right, I want the sprite to jump when I hit the space bar.

Hmm. A few pointers, even though I'm sure you'll sort this one out yourself eventually:

  • Consider having your update routine translate the player position by velocities.
  • Simulate a sort of gravity by modifying the vertical velocity a bit every time the update routine runs.
    Clamp the vertical velocity if the player is on a surface.
  • Set the vertical velocity when jump is pressed.
  • Draw the player sprite at the player position.

(And don't intertwine input, updates and drawing, if you can avoid it…)

Consider learning the basics of linear algebra to have a simple way to represent directions, velocity and/or gravity

actually I have taken a class in linear algebra a couple of years ago

thanks for the upvote

well I have got my sprite to move and jump, but only once I want it to jump every time I hit the space bar. here is my updated code.

void jumpsprite()
{
		posY = 0.0625f*(x*x);
		x += delta;
		move_sprite += 1.0f;
		if (move_sprite >= 40.0f)
		{
			move_sprite = 40.0f;
			posY = 0.0f;
		}
		if (x == min || x == max)
		{
			delta = -delta;
		}
}

void Loop(int value)
{
	jumpsprite();
	glutPostRedisplay();
	glutTimerFunc(50, Loop, 0);
}

void handleKeypress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
		break;
	case 32:
//		jumpsprite();
		Loop(0);
		break;
	}
	glutPostRedisplay();
}
 

This topic is closed to new replies.

Advertisement