Jumping?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 32
- Joined: Sat Dec 21, 2013 5:16 pm
Jumping?
I tried using several methods for jumping, but I can't get any of them to work. Any suggestions?
- Attachments
-
- pixel wizard.love
- (2.54 KiB) Downloaded 185 times
- Sheepolution
- Party member
- Posts: 264
- Joined: Mon Mar 04, 2013 9:31 am
- Location: The Netherlands
- Contact:
Re: Jumping?
I updated the code in your game.
What did I do?
The player needs to fall down by force. We call that velocity.
The velocity longer in air, the higher the velocity, so we need to add gravity to the velocity.
When you jump, you go up instead of down. And while the gravity builds up slowly, jumping is instant speed.
And then we need to know if you're standing on the ground or not.
See, it's not that hard!
What did I do?
The player needs to fall down by force. We call that velocity.
Code: Select all
velocity = 0
Code: Select all
velocity = velocity + grav * dt
Code: Select all
if jump == true then
if love.keyboard.isDown("up") or love.keyboard.isDown("w") then
player.velocity = -500
jump = false
end
end
Code: Select all
if player.y + player.h < 325 then -- We add player.h because that's his feet.
player.velocity = player.velocity + player.gravity * dt --Add gravity to the velocity
else
player.y = 325 - player.h
jump = true
velocity = 0 --Forgot to add this line in the file.
end
- Attachments
-
- pixel wizard.love
- You can actually remove the onGround variable.
- (3.23 KiB) Downloaded 192 times
Re: Jumping?
I put this in my game, and it worked great when I was using just one "ground". However, when I tried adding a ledge it gave me a syntax error "'then' expected near '=' in line 10." I dunno what's up. The map looks like this:
Code: Select all
(Player)
O ____ (ledge)
Y
_______^______________________________ (ground)
Code: Select all
if 350 < x < 456 and --ledge
y + heightsum < 412 then --above ledge
onGround = false --I know I don't need this but wtvr
velocity = velocity + gravity * dt
elseif 350 < x < 456 and --ledge
412 < y + heightsum < 464 then --in between ledge and ground
onGround = false
velocity = velocity + gravity * dt
elseif 350 < x <456 and --ledge
y + heightsum = 412 then --on ledge
Jump = true
elseif x < 350 or x > 456 and --ground
y < 464 then --above ground
onGround = false
velocity = velocity + gravity * dt
elseif x < 350 or x > 456 and --ground
y + heightsum = 464 then --on ground
Jump = true
end
Last edited by zooks97 on Thu Jan 02, 2014 4:56 pm, edited 1 time in total.
Re: Jumping?
To check of equality put an "==" not an "=".
Single "=" is for assigning values, double "==" is for checking equality.
Single "=" is for assigning values, double "==" is for checking equality.
Check out my blog on gamedev
Re: Jumping?
OoOoh. Thanks! Maybe I should actually read the syntax rules before trying to code.
Edit: Now it's telling me that I'm trying to compare a boolean with a number on line 1
Edit: Now it's telling me that I'm trying to compare a boolean with a number on line 1
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Jumping?
Also, Lua has no support for 350 < x < 456 (I think that of all the popular languages only Python supports chaining comparison operators). Replace it with 350 < x and x < 456.
Help us help you: attach a .love.
Re: Jumping?
Code: Select all
if 350 < x and x < 456 and --ledge
y + heightsum < 412 then --above ledge
velocity = velocity + gravity * dt
elseif 350 < x and x < 456 and --ledge
412 < y + heightsum and y + heightsum < 464 then --in between ledge and ground
velocity = velocity + gravity * dt
elseif 350 < x and x < 456 --and --ledge --only commented it out to actually get something to work, kind of works
--y + heightsum == 412 then --on ledge
then y = 412 - heightsum
jump = true
elseif x < 350 or x > 456 and --ground
y < 464 then --above ground
velocity = velocity + gravity * dt
elseif x < 350 or x > 456 --and --ground --only commented it out to actually get something to work, right half of map works like the ledge
--y + heightsum == 464 then --on ground
then y = 464 - heightsum
jump = true
end
Download: https://dl.dropboxusercontent.com/u/116 ... ySPv4.love
Re: Jumping?
Well, I seen your code, nice stuff you got there, but, it seems that your code is kind of messy, you might need to make separate luas for specific functions or code initialization saving your main.lua for important stuff. (That should not hamper you from making something though.
I would suggest that you learn to write variable comparisons (x > y) uniformly, that way it won't be a little hard for others to see what you do (especially if they want to help you and stuff). Try also to use constants. Since you're just repeating some stuff, might as well do such.
Anyhow, I might suggest you to use bounding boxes, they save you a lot of effort checking your collisions. Aside from that, you could also make some area vars.
See for example:
As Doc Brown in BTTF would say it, "Please excuse the crudity of this model as I didn't have time to build it to scale or paint it."
[P] is your player with an bounding box.
the x points represent a ledge area (a rectangle. X are the points of the rectangle)
the y's are ground.
You make a constant for the ground and ledge groups (eventually, you'll learn more about making functions and tables, you can make something like that with just typing 'areaMake(x1,y1,x2,y2)' or something)
Then let the player check it using a collision check. BoundingBox.lua in the wiki can be your bet. (A good search can also help too.)
That's all what my brain cells can churn for now. Others can still help you though. Good luck on what you're making!
Don't worry, I envy your char art though and - like you - I'm learning the language as well.
I would suggest that you learn to write variable comparisons (x > y) uniformly, that way it won't be a little hard for others to see what you do (especially if they want to help you and stuff). Try also to use constants. Since you're just repeating some stuff, might as well do such.
Anyhow, I might suggest you to use bounding boxes, they save you a lot of effort checking your collisions. Aside from that, you could also make some area vars.
See for example:
Code: Select all
====000===== ====000000=====
[P]
x---------------------x
| |
y--------x---------------------x--------------y
[P] is your player with an bounding box.
the x points represent a ledge area (a rectangle. X are the points of the rectangle)
the y's are ground.
You make a constant for the ground and ledge groups (eventually, you'll learn more about making functions and tables, you can make something like that with just typing 'areaMake(x1,y1,x2,y2)' or something)
Then let the player check it using a collision check. BoundingBox.lua in the wiki can be your bet. (A good search can also help too.)
That's all what my brain cells can churn for now. Others can still help you though. Good luck on what you're making!
Don't worry, I envy your char art though and - like you - I'm learning the language as well.
~ CRxTRDude || Will be out of touch for a wee longer than expected. Will keep in touch with soon enough. Sorry bout that.
Re: Jumping?
Thanks! That really helps a lot, I'll give that a go. Don't envy the character art, I stole it from an mmo I used to play, but I either won't actually release the game or will swap out textures by then if I want to. I just wanted a good looking baseline .
Who is online
Users browsing this forum: Bing [Bot] and 7 guests