MrSchmuck
MrSchmuck

Reputation: 65

Increment by 10 while key is pressed Love2D

I am extremely new to LUA and Love2D. I would like to add 10 to a variable while a key is pressed. My current code is this:

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

This works fine except it adds ten each time the key is released and pressed again. My goal is for the program to continue to add ten to the variable while the key is pressed so you will continue to move the the picture regardless of weather or not you released the key.

Upvotes: 1

Views: 208

Answers (1)

You should use the callback isDown instead of isPressed

Example:

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

Upvotes: 1

Related Questions