Advertisement

jump code

Started by September 21, 2019 04:33 PM
10 comments, last by Tom Sloper 4 years, 11 months ago

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;

}

 

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?

Advertisement

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.

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.

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 ...

 

 

Advertisement
3 minutes ago, Green_Baron said:

Programming is not your thing ...

well I am trying just for fun.

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 ?

ok I will give it my best shot

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;

}

 

This topic is closed to new replies.

Advertisement