Making Something Jump

Started by
16 comments, last by nb 16 years, 5 months ago
Your game architecture is completely flawed.

Your game runs in one continuous loop. All updates to the objects, polling of input and rendering (graphics, audio, force feedback, etc) are done within this one single loop. Now, as part of any individual action, a small loop may run, but that loop must terminate as quickly as possible, so as not to stall all the other activities in the game loop.

Because we want to localize "physical" updates, what we do is set reminders for ourselves that we intend to update certain attributes in certain ways. Flags, basically. They're particularly important for input handling, as they allow us to appear to handle multiple inputs at the same time. Example:
while(gameRunning){  ...  // this is where we obtain and process the input, but note that we   // don't update the game objects  GetKeyboardState(keys);  for(int i = 0; i < NUM_KEYS; ++i)  {    switch(keys)    {    case VK_SPACE:      if(!isJumping)      // we don't allow jumping while jumping,         isJumping = true; // and all we do is set a flag      break;    ...    }  }  ...  // here, we update the game object(s) by checking the various flags set earlier  if(isJumping)  {    // jumping code needs to operate in steps, and then as the     // final step clear the flag    step = jump(step, interval);    if(step >= interval)      isJumping = false;  }  ...}
Advertisement
Quote:Original post by Oluseyi
// here, we update the game object(s) by checking the various flags set earlier
if(isJumping)
{
// jumping code needs to operate in steps, and then as the
// final step clear the flag

step = jump(step, interval);
if(step >= interval)
isJumping = false;
}
...
}

ok, i think i'm starting to understand, but wouldn't that only run once? wouldn't you need it in a loop of some kind so it keeps changing step until it's more than interval?

honestly, the types of questions and counter-questions you are asking make me think that you are really only at a stage where you NEED to be making the mistakes you are making... but also for YOU to figure out the answers (learn why you don't put your hand on the stove etc). If at this stage you don't get an understanding within yourself of why something like this doesn't work then you are setting yourself up to be a continuous help needer because you don't really get what is and isn't actually going-on and won't have a grasp on things. Really, you are better-off just copy/pasting or using someone elses already polished code if this is the route you are heading.

but as someone else stated earlier, perhaps if you had more meaning in your question and code and described what is and isn't happening then we might be able to help more... but as it stands it really just looks like another "write my code for me" question. perhaps you could re-do your question, particularly since you have had a chance to make modifications from the original post and since you actually have multiple questions which you are spanning over different counter-questions. clear + concise = win. list your issues etc.

either way good luck.
Quote:Original post by cky83
ok, i think i'm starting to understand, but wouldn't that only run once? wouldn't you need it in a loop of some kind so it keeps changing step until it's more than interval?

while(gameRunning){  ...  if(isJumping)  {    ...  }  ...}

As you can see, it is inside a loop. Pay attention.
Quote:Original post by nb
honestly, the types of questions and counter-questions you are asking make me think that you are really only at a stage where you NEED to be making the mistakes you are making... but also for YOU to figure out the answers (learn why you don't put your hand on the stove etc). If at this stage you don't get an understanding within yourself of why something like this doesn't work then you are setting yourself up to be a continuous help needer because you don't really get what is and isn't actually going-on and won't have a grasp on things. Really, you are better-off just copy/pasting or using someone elses already polished code if this is the route you are heading.

but as someone else stated earlier, perhaps if you had more meaning in your question and code and described what is and isn't happening then we might be able to help more... but as it stands it really just looks like another "write my code for me" question. perhaps you could re-do your question, particularly since you have had a chance to make modifications from the original post and since you actually have multiple questions which you are spanning over different counter-questions. clear + concise = win. list your issues etc.

either way good luck.


well, i don't know if this helps or not, but before i even made this thread, i was able to get the guy to jump if i held down the space bar, but i'd like to have the guy jump if i just tapped the space bar.

don't know if that's what you guys are trying to show me how to do, but if it is, i guess i'm just not getting it. i'll just go to my proff tomorrow and ask him for some advice since he's the one who created most of the code i'm working with. i was just trying to get something done over the weekend, and was having trouble.

and i'm not trying to have you guys write it for me, i was looking for an explanation more than anything, and i guess i got the explanation, but i'm just not understanding it fully.
sounds like maybe u have issues elsewhere in your code too... like in your 'interval' setting in the original post, or 'time' in your last code showing. figure out a way to breakpoint it, or even better have it display somewhere constantly as the program runs (like textout to the top left corner or to a TLabel etc).

i get the feeling you aren't storing the values you think you are and so the hero will only go up when you press the spacebar as that's the only time your if condition will ever be true and all other times your variables get trashed to values you don't mean them to. it's a bit hard to tell looking only at that piece of code because you seem to be using variables outside the scope of that function, plus the naming convention is giving me a headache to try to decipher.

it seems that what you are trying to accomplish is something like

NowTime = ComputersCTimeJumpingForTime = 1000(milliseconds) //or whatever time you want or is sent to the setjumpingstate function etcJumpUntilTime = NowTime + JumpingForTime //you want to store this variable so you can keep re-testing below each time the movement code is called in the game loop...JumpTimeElapsed = JumpUntilTime - NowTimeif JumpTimeElapsed <= (JumpingForTime / 2) then hero.y += 5else hero.y -= 5


yes no?
I think his problem is a lack of understanding of object lifetimes. For instance, the isJumping flag that I used in my earlier example will reset each iteration if you declare it within the while loop, which would break the code. It's obvious to the more experienced among us that the solution is to move the declaration of isJumping outside the loop.
haha oh dear, i just looked at what i wrote and guess what... i messed that up. let's have another (better?) stab at it. keep in mind i code in delphi not C so this is dodgy and hackish and probably mixes Delphi and C and laziness conventions etc just to get the point across as best as i can manage. round 2 :

var CurrentCTimevar JumpStartTimevar JumpDurationvar JumpingBoolvar GameRunningBoolfunction SetJumping(IncomingValForJumpDuration) {  JumpStartTime = CurrentCTime  JumpDuration = IncomingValForJumpDuration  JumpingBool = 1}function MyGameLoopage {var JumpTimeElapsedvar CurrentCTime = CTime()  if (KeyDown = vk_Space) and NOT(JumpingBool) { SetJumping() }  if (KeyDown = vk_Escape) { GameRunningBool = 0 }  JumpTimeElapsed = CurrentCTime - JumpStartTime  if JumpTimeElapsed <= (JumpDuration / 2) then hero.y += 5  else hero.y -= 5  if hero.y <= 0 {    hero.y = 0    JumpingBool = 0  }}function MAIN_PROGRAM_FUNCTION {  //init cool stuff  JumpingBool = 0  GameRunningBool = 1  While (GameRunningBool) {    ProcessMessages()  //don't completely kidnap the cpu    MyGameLoopage()  //run a game cycle  }  //renderer cleanup code etc}


actually, considering what i said before about it's better for you to figure this out yourself etc etc etc... i think i've basically gone against what i said and given you pretty much exactly how i would do it right now. i wonder how close that code is to being compilable (probably not very). meh, what can i say, i screwed the first example and now i'm bored - so enjoy.

you then have other things to possibly consider like velocities etc instead of the hero (and whatever else you code) jumping in a constant up then down 8-bit-feeling manner as opposed to a fluid more realistic manner. but that's probably not something you really care about right now. it eliminates the whole "JumpingBool" toggle and adds a whole new world of complexities that make it better, but requires more code and opportunity to screw up and all just for something that seems to be meant to only be really basic.

you've mentioned your professor. what IS this all for? high school? (god forbid) uni? is it a homework assignment?

This topic is closed to new replies.

Advertisement