I attempted it with the former, and on key press, it crashes.
I've also tried it with the keypress functions inside and outside the love.update, wasn't sure about that either.
Here's my code.
Code: Select all
function love.load()
love.graphics.setMode(640,480)
height = love.graphics.getHeight()
width = love.graphics.getWidth()
gravity = 9.80665
xvel = 0
yvel = 0
maxyvel = 100
maxxvel = 100
ground = 0
xloc = 0
yloc = 0
end
function love.update(dt)
if yloc - 16 < height then
yloc = 16
end
if xloc < 0 then
xloc = 0
end
if xloc + 16 > width then
xloc = width - 16
end
xloc = xloc + xvel*dt
yloc = yloc + yvel*dt
end
function love.keypressed(key)
if key == "left" then
while xvel < maxxvel do
xvel = xvel + 1
end
end
end
function love.keypressed(key)
if key == "right" then
while yvel < maxyvel do
xvel = xvel + 1
end
end
end
function love.keyreleased(key)
if key == "left" then
while xvel > 0 do
xvel = xvel - 5
end
end
end
function love.keyreleased(key)
if key == "right" then
while xvel > 0 do
xvel = xvel - 5
end
end
end
function love.draw()
love.graphics.rectangle("line", 320, 464, 16,16)
love.graphics.print("X LOCATION = " .. xloc, 10, 10)
love.graphics.print("Y LOCATION = " .. yloc, 10, 30)
love.graphics.print("X VELOCITY = " .. xvel, 10, 50)
love.graphics.print("Y VELOCITY = " .. yvel, 10, 70)
end