2D platformer

Started by
1 comment, last by Tom Sloper 11 months, 1 week 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

fleabay said:
2 years ago, exact same question.

Agreed. Thread locked.

-- Tom Sloper -- sloperama.com

This topic is closed to new replies.

Advertisement