Hello there, I am quite new to Löve and Lua, I've only worked with Arduino before. I'm trying to make Löve register the keys W, A, S and D, however Löve crashes when either key is pressed. This is the code:
-- Controls Pressed
function love.keypressed(key)
while key == 'w' do
w_down = true
end
while key == 'a' do
a_down = true
end
while key == 's' do
s_down = true
end
while key == 'd' do
d_down = true
end
end
-- Controls Released
function love.keyreleased(key)
if key == 'w' then
w_down = false
end
if key == 'a' then
a_down = false
end
if key == 's' then
s_down = false
end
if key == 'd' then
d_down = false
end
end
-- Draw
function love.draw()
while w_down == true do
love.graphics.print('W is pressed', 400, 300)
end
while a_down == true do
love.graphics.print('A is pressed', 400, 300)
end
while s_down == true do
love.graphics.print('S is pressed', 400, 300)
end
while d_down == true do
love.graphics.print('D is pressed', 400, 300)
end
end
You are trapping Löve control flow into a endless while loop, hence the crash.
Replace those "while-do-end" with "if-then-end" clauses.
Note: This might help!
It's important to understand that your whole game is already running inside a while loop, one that constantly calls love.update and then love.draw. The only way for something to appear on the screen is if both love.update and love.draw are completed, because after that it draws everything. So you must not trap these functions in an infinite loop.