Page 2 of 2

Re: Best practice for key inputs? keypressed, isDown

Posted: Sun Oct 14, 2012 2:16 am
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.

Re: Best practice for key inputs? keypressed, isDown

Posted: Sun Oct 14, 2012 2:25 am
by stout
Because there will be a variable number of positions. This is just test code while I figure out the principles.

Re: Best practice for key inputs? keypressed, isDown

Posted: Sun Oct 14, 2012 4:54 am
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:

Re: Best practice for key inputs? keypressed, isDown

Posted: Sun Oct 14, 2012 3:02 pm
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).