• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 735
  • Last Modified:

Game PRogramming Jumping.

Im making a little game and despite some minor forgivable bugs it's going alright so far but i want to inplement jumping with both the hero and enemy's though ill worry about the controlled character for now.

Ive managed to get jumping going by following a simple tutorial targeted at game programming i have the guy jumping up and down correctly.

But......what if he's moving or not moving? i know what to do in both situations but how can i tell the two apart. Moving left or right is done via Left and Right arrow keys a var can be set so i know their moving either way but how would i tell if he's not moving?
0
Chesso
Asked:
Chesso
  • 6
  • 5
  • 3
  • +1
1 Solution
 
ErikenCommented:
I guess he wouldn't be moving if left and right arrows are unpressed and he's not up in the air?

I don't know what tutorials you've checked out, but if you take a look at Travelers' tutorials you'll get an understanding of how he does it:
    http://www.gameprogrammer.net/index.php?fuseaction=tutorial.showTutorials&lge=en

He sets player.walking := true if left or right is pressed, and player.jumping if he jumps, which is a pretty understandable way to handle it.
0
 
BlackTigerXCommented:
yes, basically that would be the concept, to keep the "status" somehow.

Game programming is a very broad topic, and there are way too many different ways to accomplish the same thing

are you using classes? records, both...?

if you are using classes and you had a property .Walking and other .Jumping for each character you could have functions SetWalking, SetJumping that would check the other Status and depending on that the value changes or not...
0
 
ChessoAuthor Commented:
Neither BlackTigerX and using keyup event it is almost possible but there's a problem with this too......

if im holding left to walk left and whilst holding it press up though i may still be holding left in it act's as if i have let go of it.

Is there a way i can work arond this? ill check out tht site now.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ChessoAuthor Commented:
Just a little more info tht might help. Im not using DirectX im using plain and simple BitBlt for drawing and sndPlaySound/MCISendString for sounds/music.

Input is just using KeyDown and KeyUp events.
0
 
BlackTigerXCommented:
well, you need to keep all those keys in an array, or each key individually

every time a key is pressed you set the respective key to true, or you can set an status that you can use
every time a key is released you set the respective key to false, and/or change a status as well

for example, I don't know what you are using, but somehow you are getting the messages and can identify which key has been pressed/released right?

so in the onPressKey event I would have something like this:

//Keys can be an array of keys, just to keep an status of all the keys
//Jumping is a boolean variable that indicates if the character is jumping
//Status can be Walking or something else (Idle or whatever)

  Keys[Key]:=True; //meaning the key is pressed

then on the KeyUp event you would have something like

Keys[Key]:=False;

that's all you need to do in those events, it is always recommended that in the events you change as little as possible relating to the logic of the game, then in the game logic (not in the events) you need to put this in your loop (every game runs in a loop)

if (Keys[VK_LEFT]) or (Keys[VK_RIGHT]) then
begin
  if not (Jumping) then
    Status:=Walking
end
else if (Keys[VK_UP]) then
  Jumping:=True

then you would handle the Jumping:=False somewhere else when you finish the jump

and so because we have 2 separate variables for "jumping" and walking", when you finish the jump if the status is Walking, the character will keep walking after finishing the jump =o), if the status if not Walking, it will not Walk after the jump

I hope you get the idea

you need other variable for the Direction

see my articles on delphi3000.com for some source code in Delphi about handling the keys in an array
http://www.delphi3000.com/articles/article_3572.asp
0
 
BlackTigerXCommented:
I missed the part to stop the character:

I modified some parts... is better this way I think

if (Keys[VK_LEFT]) or (Keys[VK_RIGHT]) then
    Status:=Walking //regardless of if is jumping or not, so if is jumping, when the jump finishes, it starts walking

if (Keys[VK_UP]) then
  Jumping:=True //allow to jump even when is walking

if not (Keys[VK_LEFT] or Keys[VK_RIGHT]) then
  Status:=Idle
0
 
ChessoAuthor Commented:
That's actually a rather cleaner way if doing it but id still be setting left/right direction moving to false because of up is pressed while either is held it activates onkeyup for either of those keys saying ive released it when i haven't.

After getting a hold on things again im going to jump straight back into DirectX now im wondering if DirectInput includes input from keyboard and does it eliminate this problem(being able to press multiple keys at once) if so then i wont worry too much about what i have right now.
0
 
DavidBirch2dotComCommented:
" using keyup event it is almost possible "

I have built several games with delphi www.davidbirch2.com 
one thing I think you need to do is to do what BlackTigerX  was saying and have a boolean varible for the down and up events and also have a timer which controls game movement (if you are using the DelphiX component set then use the timer in that) and every tick of the timer use something like this

If goingleft and not jumping then
//move left code

If goingright and not jumping then
// move right code here

If jumping then
// JUMP code

David
0
 
ChessoAuthor Commented:
atm im not suing any DelphiX and yes i am using a timer for everything.

The thing is when i hold LEFT to move left it will do this and keep walking as it should *BUT* when i press UP it releases LEFT even though im *STILL* holding it meaning a boolean is useless it would still trigger the keyup event and the boolean naturally would be set to false....
0
 
DavidBirch2dotComCommented:
if you try coding with boolean varibles  ok if you want it to still go left when jumping just use on the timer

If goingleft then
//move left code

If goingright then
// move right code here

If jumping then
// JUMP code


You should use the onkeyDOWN

if key=left then
goingleft := true;

if key = right then
goingright:= true;

AND

use on onkeyUP event

if key = left then
goingleft:= false;

ect ect

so that when each key is pushed down it starts moving when it is released it stops moving each key will work independantly of the others  AND keys can be used symaltaniously.

out of interest which component set are you using ?

David
0
 
ChessoAuthor Commented:
Component set for what? i use TTimer/TBitmap with BitBlt/PlaySound/MCISendString.

No DX or anything if that's what you mean.
0
 
DavidBirch2dotComCommented:
have you tried my suggest yet ? use the onkeydown and onkeyup evenets
0
 
ChessoAuthor Commented:
But it can't work only one key press at a time if one key is hold (LEFT) for example and then (UP) is pressed the KeyUp event will be fired so i fit s being held i won't know even if a boolean is set where would i unset it? cant be in KeyUp because it wil trigger even if im holding it in and press another key.
0
 
DavidBirch2dotComCommented:
Nope

i dont think you understand how delphi deals with onkeydown onkeyup and onkeypress events

if you pushdown a key - delphi triggers the on key down event,  when you release that key delphi triggers the onkeyUP & the onkeypress events

Delphi deals independantly with each key down and up event - each time one is pushed or released a new event happens... the idea of a boolean varible is that it stores 'IsTheKeyDownAtTheMoment' which would be set to true when the key is pushed down and set to false when the key is released  so that as each key has a varible you can check telling you if the key is down or not

If you like I can code an example of this,

David
0
 
DavidBirch2dotComCommented:
Thanks for the points :)

I hope I made sense ? tell us how you get on with your game

David
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 6
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now