Löve crashes when trying to identify key presses

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
fiskero94
Prole
Posts: 4
Joined: Sat Jan 18, 2014 8:17 pm
Location: Denmark

Löve crashes when trying to identify key presses

Post by fiskero94 »

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:

Code: Select all

-- 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
Thanks :)
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Löve crashes when trying to identify key presses

Post by Roland_Yonaba »

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!
User avatar
fiskero94
Prole
Posts: 4
Joined: Sat Jan 18, 2014 8:17 pm
Location: Denmark

Re: Löve crashes when trying to identify key presses

Post by fiskero94 »

Thank you, the code works now, however now I am even more confused about the use of "while". Is there any guides on this?
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Löve crashes when trying to identify key presses

Post by DaedalusYoung »

A while loop keeps repeating while the condition is true.

Code: Select all

x = 1
while x < 5 do
    print(x)
    x = x + 1
end
This will loop 4 times, after which x is 5 and the condition is no longer true.

See also:
* https://en.wikipedia.org/wiki/While_loop
* http://www.lua.org/pil/4.3.2.html
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Löve crashes when trying to identify key presses

Post by Plu »

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.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests