Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

jump code

Recommended Posts

well I have stubbed out my jump code. I have tried out my code and it counts up to 20 and keeps on counting 20 what I want is it to count back down to 0

#include <iostream>

using namespace std;

bool state = false;

float positionY = 0.0f;
float time = 0.0f;

int main()
{
	cout << "positionY: ";
	cin >> positionY;
	cout << endl;

	cout << "time: ";
	cin >> time;
	cout << endl;

	for (int i = 0; i < time; i++)
	{
		if (state == false)
		{
			if (positionY < 20.0f)
			{
				positionY += 5.0f;
			}
		}
		else if (state == true)
		{
			if (positionY >= 20.0f)
			{
				positionY -= 5.0f;
			}
		}

		cout << "positionY: " << positionY << endl;
	}
	system("pause");
	return 0;

}

 

Share this post


Link to post
Share on other sites
Advertisement

First question: When does your code count down to 0 ?

Second question: Knowing the above With the answer to the first question, where in the code does that condition become true?

Edited by Alberth
edited the second question

Share this post


Link to post
Share on other sites

actually it counts  up to 20 but it does not count down to 0. I want the else if statement to execute after the  first if statement executes.

Share this post


Link to post
Share on other sites
44 minutes ago, phil67rpg said:

I want the else if statement to execute after the  first

do you? are you sure? what if your object (that isn't grounded to something) have a constant gravity concept applied all the time? what would the next problem be? Fighting with the jump action. People try more power and move into a vector space instead of pixel space. Quick to get to an apex, slower to pick up speed on the way back down...those types of things.

But if we must, state is a bool. It can either be on or not. you don't need the second conditional if you are managing state with some common sense.

Share this post


Link to post
Share on other sites

The else part never executes after the if part has been executed. It is either the one or the other, the expression in round brackets is either true, then the if part is executed, or it is false, then the else part is executed.

Programming is not your thing ...

 

 

Edited by Green_Baron

Share this post


Link to post
Share on other sites

Now, you have so many hints, stop playing helpless and activate the thought machinery. Here's another hint: what must be changed in order for the awesome algorithm to count down again after it has finished counting up ?

More hints ? It is not a big change.

And no, i'll not be more specific 😉

Edited by Green_Baron

Share this post


Link to post
Share on other sites

yeah I figured out my problem, here is my code

#include <iostream>

using namespace std;

bool state = false;

float positionY = 0.0f;
float time = 0.0f;

int main()
{
	cout << "positionY: ";
	cin >> positionY;
	cout << endl;

	cout << "time: ";
	cin >> time;
	cout << endl;

	for (int i = 0; i < time; i++)
	{
		if (state == false)
		{
			if (positionY <= 20.0f)
			{
				positionY += 5.0f;
			}
			if (positionY == 20.0f)
			{
				state = true;
			}
		}
		else if (state == true)
		{
			if (positionY >= 0.0f)
			{
				positionY -= 5.0f;
			}
			if (positionY == 0.0f)
			{
				state = false;
			}
		}

		cout << "positionY: " << positionY << endl;
	}
	system("pause");
	return 0;

}

 

Share this post


Link to post
Share on other sites

I am polishing up my jumping code, I got my game to jump  up when I hit the space bar, but I want it to jump like in Mario brothers.

void spritejump()
{
		if (state == false)
		{
			if (positionY <= 20.0f)
			{
				positionY += 5.0f;
			}
			if (positionY == 20.0f)
			{
				state = true;
			}
		}
		else if (state == true)
		{
			if (positionY >= 0.0f)
			{
				positionY -= 5.0f;
			}
			if (positionY == 0.0f)
			{
				state = false;
			}
		}
}

void handleKeypress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
		break;
	case 32:
		for (int i = 0; i <= 10; i++)
		{
			spritejump();
		}
		break;

 

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!