What direction should I be looking at to achieve this?
Basically so my player/sprite will be walking on the "ground" (2d side scroller) and be able to jump and fall back down? I guess a simple question, I'm just not sure and havent seen any tutorials on this - most I have seen utilize movement all across the screen with no real "gravity"
Gravity/Jumping
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Gravity/Jumping
Just think of it as you would in real life.
First we have to make you.
Now you exist. You also have the ability to move don't you?
Ok now you can move left and right. You can also jump, so we'll add that in.
So now you can jump, but when you jump you can jump infinitely and you can repeatedly jump. So lets fix that.
So now you can jump and you fall. But you also fall through the floor. So lets add it so that doesn't happen.
And that should be everything. Now if you copy that last script in you should have a little white guy jumping on a green ground.
Thats assuming you only have a flat ground, if you want holes and need more help with horizontal collision just post here again .
Hope that helps.
Qcode
First we have to make you.
Code: Select all
function love.load()
guy = {}
guy.x = 0
guy.y = 500
guy.ySpeed = 0
guy.xSpeed = 300
guy.inAir = false
guy.length = 50
end
function love.update(dt)
end
function love.draw()
love.graphics.setColor(255, 255, 255)
love.graphics.rectangle("fill", guy.x, guy.y, 50, 50)
end
Code: Select all
function love.load()
guy = {}
guy.x = 0
guy.y = 500
guy.ySpeed = 0
guy.xSpeed = 300
guy.inAir = false
guy.length = 20
end
function love.update(dt)
if love.keyboard.isDown("left") then
guy.x = guy.x - (guy.xSpeed * dt)
end
if love.keyboard.isDown("right") then
guy.x = guy.x + (guy.xSpeed * dt)
end
end
function love.draw()
love.graphics.setColor(255, 255, 255)
love.graphics.rectangle("fill", guy.x, guy.y, 50, 50)
end
Code: Select all
function love.load()
guy = {}
guy.x = 0
guy.y = 500
guy.ySpeed = 0
guy.xSpeed = 300
guy.inAir = false
guy.length = 20
end
function love.update(dt)
if love.keyboard.isDown("left") then
guy.x = guy.x - (guy.xSpeed * dt)
end
if love.keyboard.isDown("right") then
guy.x = guy.x + (guy.xSpeed * dt)
end
guy.y = guy.y + (guy.ySpeed * dt)
end
function love.draw()
love.graphics.setColor(255, 255, 255)
love.graphics.rectangle("fill", guy.x, guy.y, 50, 50)
end
function love.keypressed(key)
if key == "up" then
guy.ySpeed = guy.ySpeed - 200
end
end
Code: Select all
function love.load()
guy = {}
guy.x = 0
guy.y = 500
guy.ySpeed = 0
guy.xSpeed = 300
guy.inAir = false
guy.length = 20
end
function love.update(dt)
if love.keyboard.isDown("left") then
guy.x = guy.x - (guy.xSpeed * dt)
end
if love.keyboard.isDown("right") then
guy.x = guy.x + (guy.xSpeed * dt)
end
guy.y = guy.y + (guy.ySpeed * dt)
if guy.inAir then
guy.ySpeed = guy.ySpeed + (100 * dt)
end
end
function love.draw()
love.graphics.setColor(255, 255, 255)
love.graphics.rectangle("fill", guy.x, guy.y, 50, 50)
end
function love.keypressed(key)
if key == "up" and not guy.inAir then
guy.ySpeed = guy.ySpeed - 200
guy.inAir = true
end
end
Code: Select all
function love.load()
guy = {}
guy.x = 0
guy.y = 500
guy.ySpeed = 0
guy.xSpeed = 300
guy.inAir = false
guy.length = 20
end
function love.update(dt)
if love.keyboard.isDown("left") then
guy.x = guy.x - (guy.xSpeed * dt)
end
if love.keyboard.isDown("right") then
guy.x = guy.x + (guy.xSpeed * dt)
end
guy.y = guy.y + (guy.ySpeed * dt)
if guy.inAir then
guy.ySpeed = guy.ySpeed + (100 * dt)
end
if guy.y + guy.length > 500 then
guy.y = 500 - guy.length
guy.inAir = false
end
end
function love.draw()
love.graphics.setColor(255, 255, 255)
love.graphics.rectangle("fill", guy.x, guy.y, 50, 50)
love.graphics.setColor(0, 255, 0)
love.graphics.rectangle("fill", 0, 500, 800, 300)
end
function love.keypressed(key)
if key == "up" and not guy.inAir then
guy.ySpeed = guy.ySpeed - 200
guy.inAir = true
end
end
Thats assuming you only have a flat ground, if you want holes and need more help with horizontal collision just post here again .
Hope that helps.
Qcode
Re: Gravity/Jumping
Nice tutorial but you may have some unnoticed quirks since you using different sizes for height. the guy.length is 20 but the draw guy size is 50. So the guy is not jumping on a flat green ground, but stuck and jumping in the middle of green mud (because you drawing also terrain after/over player).Qcode wrote: And that should be everything. Now if you copy that last script in you should have a little white guy jumping on a green ground.
Thats assuming you only have a flat ground, if you want holes and need more help with horizontal collision just post here again .
Hope that helps.
Qcode
So a next evolution in code could be add width/height to guy (and replace lenght) and auto-relate it (without starting fixed position numbers) with the ground (that also is now auto-draw related to screen):
Code: Select all
function love.load()
ground = {}
ground.width = love.graphics.getWidth()
ground.height = 100
ground.x = 0
ground.y = love.graphics.getHeight() - ground.height
guy = {}
guy.width = 50
guy.height = 50
guy.ySpeed = 0
guy.xSpeed = 300
guy.inAir = false
guy.x = love.graphics.getWidth()/2 - guy.width/2
guy.y = ground.y - guy.height
end
function love.update(dt)
if love.keyboard.isDown("left") then
guy.x = guy.x - (guy.xSpeed * dt)
end
if love.keyboard.isDown("right") then
guy.x = guy.x + (guy.xSpeed * dt)
end
guy.y = guy.y + (guy.ySpeed * dt)
if guy.inAir then
guy.ySpeed = guy.ySpeed + (100 * dt)
end
if guy.y + guy.height > 500 then
guy.y = 500 - guy.height
guy.inAir = false
end
end
function love.draw()
love.graphics.setColor(0, 255, 0)
love.graphics.rectangle("fill", ground.x, ground.y, ground.width, ground.height)
love.graphics.setColor(255, 255, 255)
love.graphics.rectangle("fill", guy.x , guy.y, guy.width, guy.height)
end
function love.keypressed(key)
if key == "up" and not guy.inAir then
guy.ySpeed = guy.ySpeed - 200
guy.inAir = true
end
end
Re: Gravity/Jumping
My last attempt at a platform game failed miserably .Whoops. I wrote all of it in the forum then just copied it in to test. I fixed that mistake, though evidently forgot to move it over. We could improve it some more, and add some acceleration on the xSpeed though I think all he wanted to know was how to jump.
Who is online
Users browsing this forum: Ahrefs [Bot] and 5 guests