按下键时增加 10 Love2D

Increment by 10 while key is pressed Love2D

我对 LUA 和 Love2D 非常陌生。我想在按下某个键时将 10 添加到变量中。我当前的代码是这样的:

local y
local x
local test 
local key

function love.load()
   y = 0
   x = 0
   test = love.graphics.newImage("test.jpg")
end

function love.draw()
   love.graphics.draw(test, x, y)
end

function love.update(dt)

end

function love.keypressed( key )
    if key == "down" then
        y = y+10
    end
    if key == "up" then
        y = y-10
    end
    if key == "left" then
        x = x-10
    end
    if key == "right" then
        x = x+10
    end
end

除了每次释放并再次按下键时它会增加 10 之外,这工作正常。我的目标是让程序在按下键时继续向变量添加 10,这样无论天气如何,您都可以继续移动图片。您是否松开键。

您应该使用回调 isDown 而不是 isPressed

示例:

if love.keyboard.isDown( " " ) then.
      text = "The SPACE key is held down!" 
end