Search code examples
functionmenulualove2d

Love2D Menu select


For some reason the code is not responding when i got the code if love.keyboard.isDown("s") Maybe i am using the functions just wrong but its still weird.

I tried using the Menu() function in love.keypressed and in love.update. Still no response.

Here is the full code:

main.lua

function love.load()
require "menu"
select = love.graphics.newImage("select.png")
Menu()
end
function love.draw()
   Menu()
end
function love.keypressed(key)
   Menu()
end

function love.update()
   Menu()
end

menu.lua

function Menu()
sly = 300
love.graphics.draw(select,sly,300)
if key == "w" then
    sly = sly + 50
end

if love.keyboard.isDown("s") then
    sly = sly - 50
end
end

Solution

  • Not sure what exactly you are trying to do here, but in your Menu() function you are resetting the sly variable every time you call it, and then modifying sly after you have already drawn the image. This results in nothing happening.

    If you change it so that sly is initialized outside Menu(), it at least moves the image when you press the 's' key. Also, if you wanted 's' to move it one way and 'w' to move it the other way, maybe the code should be like this:

    sly = 300
    
    function Menu()
    
        love.graphics.draw(select,sly,300)
        if love.keyboard.isDown("w") then
            sly = sly + 50
        end
    
        if love.keyboard.isDown("s") then
            sly = sly - 50
        end
    end
    

    Or you could pass the key variable into Menu() when you call it from love.keypressed and not use the keyboard.isDown function.

    Again, I'm not sure what you are really trying to do here, but it looks to me like the code is working fine, you may just have a logic error.