Jump to content
MakeWebGames

Jumping Effect & Gravity


Script47

Recommended Posts

So I decided to try something (which I haven't tried before) I have *attempted* to make gravity/jumping, it can be a big part of quite a few games (I mean who doesn't like jumping). It isn't perfect but I thought it was decent for first attempt. So gimmie your ideas, what you think. And any other advice.

Edit 1:

Any ideas how I could cap the insane jumping time? So user can't just keep hold of it and halo jump forever? I tried a counter but that had unexpected (and quite annoying) results.

Jumping Effect (Gravity)

Edited by Script47
Link to comment
Share on other sites

Im not near a jQuery expert but did you just try the key up function for the spacebar rather than both the keyup and keydown?

I am not using jQuery, I'm using plain JavaScript. No other libraries - jQuery is a library of JavaScript. I'm trying to keep it JavaScript only.

As for the second part, no. Because the flow of the example runs like this:

 

  • Code checks if user is pressing up key (keydown)
  • If key is pressed (keydown still) and has not been released (keyup) then we move upwards
  • However if key has been released (keyup) then we introduce gravity

 

If I did keyup it would move the target (in this case the black square) after you have released the key which is not how it goes. I'm not sure if that's clear enough but tell me if it isn't I'll try explain better.

Thanks for the idea anyway. :)

Link to comment
Share on other sites

Looks good! Find it weird that you have to hold down the space bar to continue jumping. You should set a fixed height for each jump.

Yes, I have been given an idea from someone which I am going to try. Which basically is a variable with a bool value set by default as true, this check if you're on the ground, when you jump it will turn false and you won't be able to jump again until that value is set to false.

Link to comment
Share on other sites

Yes, I have been given an idea from someone which I am going to try. Which basically is a variable with a bool value set by default as true, this check if you're on the ground, when you jump it will turn false and you won't be able to jump again until that value is set to false.

That'd work! Providing the variable never comes out of sync.

A potentially better (less hackable, but slightly more intensive way) would be to simply check the users position on the view. If it's not 0 (presuming this is the floor) they're jumping!

Link to comment
Share on other sites

Why not do it so on jump you set a vertical velocity (-v, up) and decrement it to (+v, down) & in your loop you can just do velocity.y += gravity with gravity being some float.

My idea wasn't tested but I can't see why it won't work. You need to think about the logic of an actual jump and program it that way.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...