I finally got jumping and gravity to work, but now I've got another problem to face. I can only jump once! Dx I have no idea as to why this is happening. Help pls?
debug = false
function love.load()
player = {}
player.y = 500
player.x = 100
player.w = 50
player.h = 50
player.b = player.y + player.h
player.r = player.x + player.w
player.y_vel = -600
player.state = "idle"
ground = {}
ground.y = 550
ground.w = love.graphics.getWidth()
ground.h = love.graphics.getHeight() - ground.y
presskey = "none"
releasekey = "none"
gravity = 1000
end
function love.update(dt)
-- if love.keyboard.isDown("up") and not player.state == "jumping" then
if presskey == "up" then
player.state = "jumping"
end
if player.state == "jumping" then
player.y = player.y + player.y_vel * dt
player.y_vel = player.y_vel + gravity * dt
if player.y + player.h > ground.y then
player.y = ground.y - player.h
player.state = "idle"
presskey = "none"
end
end
end
function love.draw()
love.graphics.rectangle("fill",player.x,player.y,player.w,player.h)
love.graphics.rectangle("fill",0,ground.y,ground.w,ground.h)
-- if debug then
love.graphics.print(player.state,50,50,0,1,1)
love.graphics.print(player.y,50,60,0,1,1)
love.graphics.print(presskey,50,70,0,1,1)
-- end
end
function love.keypressed(key)
if key == "up" then
presskey = "up"
end
end
function love.keyreleased(key)
if key == "`" then
debug = not debug
end
if key == "up" then
releasekey = "up"
end
end
if player.state == "jumping" then
player.y = player.y + player.y_vel * dt
player.y_vel = player.y_vel + gravity * dt
if player.y + player.h > ground.y then
player.y = ground.y - player.h
-- Here is what you should add !! --
player.y_vel = -600
-- That's all ;) --
player.state = "idle"
presskey = "none"
end
end
I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.
Lynesth wrote:
It seems taht you simply forgot to reset player.y_vel to -600 when player is back in idle state.
Yeah but I'm personally not a fan that hardcoded value and it's not great that he put fixed values inside the jumping routine so I suggest some simple changes.
I simplified the key routines and cleaned not needed variables like "presskey"/"releasekey". I think it's simpler now.
Some other optimizations could be made. I would also make a simpler player.jumping Boolean instead of player.state "idle"/"jumping" but I didn't want change too much.
debug = true
function love.load()
ground = {}
ground.y = 550
ground.w = love.graphics.getWidth()
ground.h = love.graphics.getHeight() - ground.y
player = {}
player.y = 500
player.x = 100
player.w = 50
player.h = 50
player.b = player.y + player.h
player.r = player.x + player.w
player.y_vel_base = -(ground.y + player.h)
player.y_vel = player.y_vel_base
player.state = "idle"
gravity = 1000
end
function love.update(dt)
if love.keyboard.isDown("up") and player.state == "idle" then
player.state = "jumping"
end
if player.state == "jumping" then
player.y = player.y + player.y_vel * dt
player.y_vel = player.y_vel + gravity * dt
if player.y + player.h > ground.y then
player.y = ground.y - player.h
player.y_vel = player.y_vel_base
player.state = "idle"
end
end
end
function love.draw()
love.graphics.rectangle("fill",player.x,player.y,player.w,player.h)
love.graphics.rectangle("fill",0,ground.y,ground.w,ground.h)
if debug then
love.graphics.print(player.state,50,50,0,1,1)
love.graphics.print(player.y,50,60,0,1,1)
end
end
function love.keypressed(key)
end
function love.keyreleased(key)
if key == "tab" then
debug = not debug
end
end
coffee wrote:Some other optimizations could be made. I would also make a simpler player.jumping Boolean instead of player.state "idle"/"jumping" but I didn't want change too much.
I'm going to add other states like running, attacking and blocking, so I thought I might as well just put them all into one variable.
also, do you know why if I change player.y + player.h to player.b, that the collision doesn't happen anymore? :S
onedaysnotice wrote:
I'm going to add other states like running, attacking and blocking, so I thought I might as well just put them all into one variable.
Yes, but that is the problem and you didn't notice the issues you get in.
So let's examine it.
image that your player is not jumping but also isn't idle or quiet like is moving to right. Does player player.status = "idle" make sense? No. So you change to player.status = "walking_right" Now imagine that you are "walking_right" but you jump too! Ops! Since your player is doing simultaneous things you can't use player.status for different things. Or you keep "walking_right" or you loose your "jump" detection. See the problem?
I would suggest