How do I make my player move repeatedly after I press an arrow key?
Posted: Sat May 14, 2016 4:43 am
Hi, I'm new to the fourms.
I'm getting started In LOVE2D and I want to make my player to keep moving after I press an arrow key (Like Snake but with only one square) Instead of It stop moving after I let go of the arrow key.
This Is the whole source btw:
I'm getting started In LOVE2D and I want to make my player to keep moving after I press an arrow key (Like Snake but with only one square) Instead of It stop moving after I let go of the arrow key.
This Is the whole source btw:
Code: Select all
function love.load()
hero = {}
hero.x = 0 -- the position of the player
hero.y = 0
hero.speed = 200 -- the speed of the player
l = 0 -- to detect what key the player last pressed/ left
r = 0 -- right
u = 0 -- up
d = 0 -- down
end
function love.update(dt)
-- controls
if love.keyboard.isDown("left") then
hero.x = hero.x - hero.speed*dt
l = 1
r = 0
u = 0
d = 0
elseif love.keyboard.isDown("right") then
hero.x = hero.x + hero.speed*dt
l = 0
r = 1
u = 0
d = 0
elseif love.keyboard.isDown("up") then
hero.y = hero.y - hero.speed*dt
l = 0
r = 0
u = 1
d = 0
elseif love.keyboard.isDown("down") then
hero.y = hero.y + hero.speed*dt
l = 0
r = 0
u = 0
d = 1
end
end
function love.draw()
-- hero/player
love.graphics.setColor(175, 25, 134, 255) -- sets colour of player
love.graphics.rectangle("fill", hero.x, hero.y, 55, 55) -- makes player drawn/visible
-- debug en la gamo/ debug in the game
love.graphics.setColor(0,0,0,255)
love.graphics.newFont(18)
love.graphics.setNewFont(18)
love.graphics.print(hero.x, 1100, 700)
love.graphics.print(hero.y, 1100, 650)
love.graphics.print("x", 1080, 700)
love.graphics.print("y", 1080, 650)
love.graphics.print(l, 1100, 450)
love.graphics.print("l", 1080, 450)
love.graphics.print(r, 1100, 500)
love.graphics.print("r", 1080, 500)
love.graphics.print(u, 1100, 550)
love.graphics.print("u", 1080, 550)
love.graphics.print(d, 1100, 600)
love.graphics.print("d", 1080, 600)
-- background
love.graphics.setBackgroundColor(50,200,0) -- so It dosn't look so creepy black
end