Public Group

# Making Something Jump

This topic is 3977 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

sounds simple, but i'm having trouble. right now i have something like:
void movey(int interval)
{
float k; int h = 0;
for (int i=0;i<m_nCount;i++)
{
int j=m_pObjectList->m_nObjectType;

//3 is the enumerated type that coicides with the HERO object

if (j==3)
{
k=m_pObjectList->m_structLocation.y;

while ( h <= interval )
{
if ( h < interval/2)
{
m_pObjectList->m_structLocation.y += 5;
}else{
m_pObjectList->m_structLocation.y -= 5;
}
h++;
}
}
}
} 
and it doesn't work, and i don't know why. i know it's not optimal, so, yeah, any help you guys could give me would be awesome.

##### Share on other sites
Do you get eroor's of some kind? If you do, then could you post them?

##### Share on other sites
I've got some mean comments here, please don't take it the wrong way. My intention is simply to correct some bad habits. :)
So, what is k? What is h? You should prefer more descriptive names - otherwise code will be puzzling you a few months later. And why not use an enum instead of a magical number (j == 3)? Also, what's with the m_ and p prefixes? Doesn't your IDE tell you that it's a member variabele and a pointer already? As for pointers, are you familiar with the STL - the standard template library, with it's convenient container classes such as std::vector?

Anyway, it doesn't really look efficient, iterating through a list of game objects only to update the hero's vertical position. Personally, I'd do that inside the hero's own update function, since I'd be updating all of my game objects anyway, so this isn't really a special case that needs to be handled separately. Depends on the game though, but if you really need to handle your hero seperately, why not store a reference to it directly, instead of having to look it up in a list?

As for why the jumping doesn't work, 'it doesn't work' is vague. What exactly is it supposed to do, and what exactly is - and isn't - it doing? Did you step through the code with a debugger already to see where things could possibly go wrong?

Another note: it looks like your jumping code affects the position of the hero directly. This results in a rather unconvincing look: at some point, the hero will immediatly start falling with exactly the same speed. For a more natural look, use a velocity value, and modify that one over time, while modifying the position with the velocity value.

Oh, and this forum supports a source tag, which allows easier code formatting and provides basic syntax highlighting. Makes posts containing code easier to read. :)

##### Share on other sites
Quote:
 Original post by cky83and it doesn't work, and i don't know why.

Let's step back for a while and talk about program flow.

Your program begins at main (or WinMain for unwrapped Windows applications), and runs straight down until it encounters a function call, a loop, or a conditional branch (if statement). If it encounters a function call, flow transfers to the top of that function and runs just like in main until it encounters a return statement or the end of the function.

In all likelihood, you have something like this:
int main(){  ...  while(someCondition)  {    // stuff    render();    // more stuff  }  return 0;}

This causes your rendering code to be executed once per loop iteration. Now, if your function movey is called inside that loop, your rendering code won't get called until the function is finished. What's the problem with that? Your function movey contains a loop which runs over the span of your "jump", without exiting once to let render get called.

You need a way to set the object's state to "jumping", and have it go through all the motions in steps over several iterations of your game loop, but without going off into another function and never coming back. One way to do that is to make your function progress in steps:
int movey(int ObjectIndex, int currentStep, int interval){  if(currentStep < interval / 2)  {    m_pObjectList[ObjectIndex]->m_structLocation.y += 5;  } else {    m_pObjectList[ObjectIndex]->m_structLocation.y -= 5;  }  return ++currentIndex;}

Use it something like this:
int main(){  ...  int jump_step;  ...  while(someCondition)  {    ...    if(object_is_jumping)    {      jump_step = movey(objIndex, jump_step, interval);    }    ...  }}

##### Share on other sites
Well one problem is that you're doing it in a while loop. What's gonna happen is its gonna jump and you'll have no idea because there's no rendering of the jump going on, you only want to update the player's location once per frame.

##### Share on other sites
i used to have it looking like this:

void movey(int time){	float k; int t = g_cTimer.time();	for (int i=0;i<m_nCount;i++)	{		int j=m_pObjectList->m_nObjectType;		//3 is the enumerated type that coicides with the HERO object		if (j==3)		{			k=m_pObjectList->m_structLocation.y;			while(!g_cTimer.elapsed(t, time){				if(!g_cTimer.elapsed(t, time/2)  ){					m_pObjectList->m_structLocation.y += 1;				}else{					m_pObjectList->m_structLocation.y -= 1;				}			}//while		}//if	}//for}

where g_cTimer.time() returns the current time, and g_cTimer.elapsed returns TRUE if the amount of time designated by int time has elapsed since t, and returns FALSE if it hasn't.

this ended up making the character disspear, tho.

Quote:
 Original post by Dancin_FoolWell one problem is that you're doing it in a while loop. What's gonna happen is its gonna jump and you'll have no idea because there's no rendering of the jump going on, you only want to update the player's location once per frame.

how would i do that?

##### Share on other sites
Sigh. This is something that has been bothering me lately. Someone comes asking for help, they get a bunch of replies, one of which contains the answer and the OP for some unknown reason decides to ignore it completely, reply to the latest reply and ask for something that was already answered.

##### Share on other sites
Original post by Oluseyi
Quote:
 int movey(int ObjectIndex, int currentStep, int interval){ if(currentStep < interval / 2) { m_pObjectList[ObjectIndex]->m_structLocation.y += 5; } else { m_pObjectList[ObjectIndex]->m_structLocation.y -= 5; } return ++currentIndex;}Use it something like this:int main(){ ... int jump_step; ... while(someCondition) { ... if(object_is_jumping) { jump_step = movey(objIndex, jump_step, interval); } ... }}

ok, i see what's going on there.

i've got a few questios, tho.

1) what is ++currentIndex reffering to?

2) in the while(someCondition), what would someCondition be?

i have a switch case in main that says
case VK_SPACE: g_cObjectManager.movey(200);

where ObjectManager handles things like the character moving and jumping.

the switch case is not located inside a while loop, it gets called anytime the space is pressed.

3) in this example, what would be the case to see if object_is_jumping is true?

##### Share on other sites
ok, i think i've done something like that, here's what i have now:

void ObjectManager::jump(int time){	int step = 0;	int t = g_cTimer.time();   	while(!g_cTimer.elapsed(t, time)){		step = jump(step, 10);	}//while}int ObjectManager::jump(int current, int interval){	if (current < interval/2){		m_pObjectList[m_nHeroIndex]->m_structLocation.y += 5;	}else{		m_pObjectList[m_nHeroIndex]->m_structLocation.y -= 5;	}	return ++current;}

and what will happen is that after time amount of time has passed, the character will dissappear.

NOTE: Timer.time() gets the current time, and Time.elapsed(int, int) will return TRUE if the time between t and time has elapesed, and FALSE if it has not.

also, like i said above, jump(int time) will only get called if the space bar is pressed, due to the switch statement in main.

##### Share on other sites
Quote:
 Original post by cky83void ObjectManager::jump(int time){int ObjectManager::jump(int current, int interval){

these two methods are the same name, one is for time while the other is input values for current and interval? if they are the same method you need: jump(int time, int) when using jump(time, ). Only reason I ask they are different is one is void and the other is returns a int.

abrev. names are fine but if you dont know what they are then make them descriptive. bigDogTailWags(). smallDogTailWags(). jumpTimeStart(), jumpTimStp(). This helps other people help you...

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 48
• ### Forum Statistics

• Total Topics
632637
• Total Posts
3007578
• ### Who's Online (See full list)

There are no registered users currently online

×