Keydown problems?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Afr0
Prole
Posts: 19
Joined: Mon Feb 22, 2010 9:01 am

Keydown problems?

Post by Afr0 »

Ok, so this is my code that I started yesterday;

Code: Select all

function love.load()
	love.graphics.setBlendMode('alpha')
	love.keyboard.setKeyRepeat(100, 100)
	
	PlayerShip = love.graphics.newImage("PlayerShip.png")
	PlayerShipX = 350
	PlayerShipY = 100
end

function love.keypressed(k)
	if k == 'escape' then
		love.event.push('q') --Quits the game
	end
end

function love.update()
	if love.keyboard.isDown('up') then
		PlayerShipY = PlayerShipY - 1
	end
	
	if love.keyboard.isDown('down') then
		PlayerShipY = PlayerShipY + 1
	end
	
	if love.keyboard.isDown('left') then
		PlayerShipX = PlayerShipX - 1
	end
	
	if love.keyboard.isDown('right') then
		PlayerShipX = PlayerShipX + 1
	end
end

function love.draw()
	love.graphics.toggleFullscreen()
	love.graphics.draw(PlayerShip, PlayerShipX, PlayerShipY)
end
There's really only one problem with this code... love.keyboard.isDown() doesn't seem to register keydowns, but rather it seems to register keypresses! I.E my spaceship doesn't move when I hold my key down, only when I press it continiously. Any suggestions?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Keydown problems?

Post by Robin »

I've never heard of anything like that... does that still happen when you remove the setKeyRepeat line?

(As a side note: do you know about dt? I recommend you to use it, because it will help to provide a consistent experience, independent from what computer the player uses.)
Help us help you: attach a .love.
Afr0
Prole
Posts: 19
Joined: Mon Feb 22, 2010 9:01 am

Re: Keydown problems?

Post by Afr0 »

Yeah it still happens if I remove that line. I added that line because I figured it might solve the issue, but it didn't help at all. I have no idea what setKeyRepeat() actually does, either. The documentation wasn't very good.
Hm... I should probably at least add dt as an argument to the update function! :)
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Keydown problems?

Post by bartbes »

This isn't odd AT ALL, let me explain it to you.
love.keypressed is called whenever a key is pressed, once for every key, and the key repeat just calls the keypressed callback again after a while.
love.keyboard.isDown works as you want it to, but it has to be run every frame for it to work (the way you intend it to), and since it is called from within keypressed, it isn't called every frame.
To do what you want you should move the key checking code (with the isDown calls) to love.update, add a dt argument and change + 1 to + dt.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Keydown problems?

Post by TechnoCat »

toggleFullscreen() should not be here.

Code: Select all

function love.draw()
   love.graphics.toggleFullscreen()
   love.graphics.draw(PlayerShip, PlayerShipX, PlayerShipY)
end
It is calling toggleFullscreen every frame this way. Also, it appears to be blocking out your key repeats.
Afr0
Prole
Posts: 19
Joined: Mon Feb 22, 2010 9:01 am

Re: Keydown problems?

Post by Afr0 »

Ah, yes! :)

Moving toggleFullscreen() and adding dt seems to have done the trick
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 35 guests