I'm currently meeting a rather embracing problem with a "game". I can't handle properly jumping and falling. It seems like the character is shivering when he walks on the ground. Here's the code :
Code: Select all
--Global Variables
TILE = 64
HEIGHT = TILE * 7
WIDTH = TILE * 10
GRAVITE = 0
function love.keypressed(key)
if key == " " then
GRAVITE = 500
giorgio.v_y = -200
end
end
function love.load()
--Creation of the level
local f = io.open("level.lvl", "r")
level = f:read("*all")
f:close()
--Window settings
love.graphics.setMode(WIDTH, HEIGHT)
love.graphics.setCaption("Italiano, si ?")
--Importing images
giorgioImg = love.graphics.newImage("Giorgio.png")
terreImg = love.graphics.newImage("Terre.png")
giorgio = {x = 0,
y = HEIGHT-TILE-giorgioImg:getHeight()-1,
v_x = 200,
v_y = 0,
w = giorgioImg:getWidth(),
h = giorgioImg:getHeight()
}
decor = {}
end
function love.update(dt)
if love.keyboard.isDown("right") then
giorgio.x = giorgio.x + giorgio.v_x*dt
elseif love.keyboard.isDown("left") then
giorgio.x = giorgio.x - giorgio.v_x*dt
end
for i, v in ipairs(decor) do
if (giorgio.x + giorgio.w < v.x or giorgio.x > v.x + TILE or giorgio.y + giorgio.h < v.y or giorgio.y > v.y+1 + TILE) then
GRAVITE = 500
else
giorgio.y = giorgio.y - giorgio.v_y*dt
GRAVITE = 0
giorgio.v_y = 0
end
end
giorgio.y = giorgio.y + giorgio.v_y*dt
giorgio.v_y = giorgio.v_y + GRAVITE*dt
end
function love.draw()
love.graphics.draw(giorgioImg, giorgio.x, giorgio.y)
--Foreground elements
i = 0; j = 0
for c in level:gmatch(".") do
if c == '1' then
terre = {x = TILE*i, y =TILE*j}
table.insert(decor, terre)
love.graphics.draw(terreImg, TILE*i, TILE*j)
i = i + 1
elseif c == '0' then
i = i + 1
elseif c == '\n' then
j = j + 1
i = 0
end
end
end
Does anyone know how to handle jumping and falling properly ?