๐ŸŽ‰ 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!

2D platformer

Started by
2 comments, last by Tom Sloper 1ย year ago

I have got my code to jump up and down continuously when I press the space bar How do I get my sprite to jump up and down only once when I hit the space bar.

float velY = 0.0f;
float velX = 0.0f;
float gravity = 0.5f;
bool onGround = false;

void StartJump()
{
	if (onGround)
	{
		velY = 12.0f;
		onGround = false;
	}
}

void EndJump()
{
	if (velY < 6.0f)
	{
		velY = 6.0f;
	}
}

void Update()
{
	velY += gravity;
	y += velY;
	x += velX;
	if (y <= 0.0f)
	{
		y = 0.0f;
		velY = 0.0f;
		onGround = true;
	}
}

void jump()
{
	x--;
	y = (-0.1f * (x * x)) + 50;
	if (x <= -22.5)
	{
		x = 22.5;
	}
	glutPostRedisplay();
	cout << x << " " << y << endl;
}

void Render()
{
	jump();
}

void Loop(int val)
{
	Update();
	Render();
//	glutTimerFunc(50, Loop, 0);
}

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

2 years ago, exact same question.

https://www.gamedev.net/forums/topic/710008-jumping-sprite/

๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚<โ†The tone posse, ready for action.

fleabay said:
2 years ago, exact same question.

Agreed. Thread locked.

-- Tom Sloper -- sloperama.com

This topic is closed to new replies.

Advertisement