Best practice for key inputs? keypressed, isDown

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.
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: Best practice for key inputs? keypressed, isDown

Post by Qcode »

So, wait you want the selectorY to just switch between 45, 95, and 145? If you do, why don't you just do this?

Code: Select all

selectorY = 45

functs[ #functs+1 ] =

   function(key)
         if key == "up" then
            if selectorY == 95 or selectorY == 145 then
               selectorY = selectorY - 50
            end
         end

         if key == "down" then
            if selectorY < 145 then
               selectorY = selectorY+50
            end
         end
     end
That worked for me.
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

Re: Best practice for key inputs? keypressed, isDown

Post by stout »

Because there will be a variable number of positions. This is just test code while I figure out the principles.
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: Best practice for key inputs? keypressed, isDown

Post by Helvecta »

Hi Stout! I'm just going by your first post, where you said that the following doesn't work:
stout wrote:

Code: Select all

function love.keypressed(key)
     titleKeys()
end

function titleKeys()
		if key == "n" then
			view = "map"
      end
end
I'm not sure if this is still relevant, but it is very possible! Just pass the "key" variable to the new function! Here's a short example:

Code: Select all

view = "normal"

function love.keypressed(key)
     titleKeys(key)
end

function titleKeys(key)
      if key == "n" then
         view = "map"
      end
end

function love.draw()
	if view == "map" then
		love.graphics.print("WHAT, THE MAP IS OPEN!!!", 10, 10)
	elseif view == "normal" then
		love.graphics.print("Press the n key, babe.", 10, 10)
	end
end
If this doesn't help you at all, sorry! But reading that forced me to go test it right away.

Hope you find your answer! :crazy:
"Bump." -CMFIend420
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

Re: Best practice for key inputs? keypressed, isDown

Post by stout »

Helvetica: Thanks! Yes, that works just fine, and is what I wanted to do. It also fixes my selector problem so that works like I expect it to now. Still curious what was causing the bug with the other code, but the new code works great and finally hammers how passing arguments to functions work (I am, it seems, a very slow learner).
Post Reply

Who is online

Users browsing this forum: No registered users and 197 guests