how do I get my sprite to move up and down once

Started by
67 comments, last by Tom Sloper 4 years, 6 months ago

I have worked on this problem for several days. I have almost solved my problem. I have got my sprite to move up and  down continuously using the mouse. this done by  using the glutTimerFunc function. I want the mouse to get the sprite to move up and down once. I have googled and debugged my code. I have also taken some JavaScript code and ported it over into opengl. let me  know if you need my  question to be more specific. here is my code so far.


bool onGround = false;

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

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

void Update()
{
	velocityY += gravity;
	positionY += velocityY;
	positionX += velocityX;
	if (positionY > 25.0f)
	{
		positionY = 0.0f;
		velocityY = 0.0f;
		onGround = false;
	}
}

void Loop(int val)
{
	Update();
	drawSprite();
	glutPostRedisplay();
	glutTimerFunc(33, Loop, 0);
}

void mouseClicks(int button, int state, int x, int y)
{
	if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
	{
		StartJump();
	}
	if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
	{
		EndJump();
	}
}

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

 

Advertisement

You didnt ask a question. Also a quick screenshot or video on youtube might help.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

 my question is  how do I get my  sprite to move up and down once instead of moving up and down continuously. https://imgur.com/a/3E9YlFn

You're moving in the right direction, Phil. You must set onGround to true in your Update function, because that's when you land.

Also StartJump and EndJump need work, remember that these functions only get called 1 time when you actualy press/release the button. You probably don't need EndJump.

Edit: I removed some thoughts about glutTimerFunc because it seems that is the way glut works. As others have said, glut may not be the best framework to learn common game programming concepts.

thanks prototype I like freeglut because it is what I used in college. I changed onGround to true in Update function. it still  moves  up and down continuously.  

1. Find the code that makes it stop jumping.

2. Figure out why that doesn't work, for starters, decide if that code is called at the right time.

 

Code doesn't have a mind of it own, everything it does or doesn't do has been programmed (or not programmed) by you.

In all cases, find the code that is responsible for the bad behavior, and find out why it doesn't do what it is supposed to be doing.

20 hours ago, Prototype said:

Also StartJump and EndJump need work, remember that these functions only get called 1 time when you actualy press/release the button. You probably don't need EndJump.

can  you be more specific

2 hours ago, phil67rpg said:

can  you be more specific

He doesn't need to be more specific. You need to analyze your code, step through it, and figure out why it doesn't work.He told you where to look.

-- Tom Sloper -- sloperama.com

ok I will work on my code today, I think my problem is that I don't  understand the glutTimerFunc function very well.

Okay, make sure you get to understand it well because it's vital to be able to follow the flow of your program. If you can run the code inside your head things will become much easier to figure out. Using a framework that runs on asynchronous timers doesn't really help in that regard.

You know, there are frameworks out there that let you do this kind of thing with six lines of code. You have to ask yourself what it is you actually want to achieve. Do you want to make a finished game? Do you just want to muck around with openGL? Think about that, and then pick the tools that make it possible in the easiest way. You don't learn much from entering dead-end roads all the time.

This topic is closed to new replies.

Advertisement