What I want to do is make the character move faster and when the button is released he goes back to his normal speed. I got him to move fast but when i release the button he does not go back to his original speed. The code will be below.
Code: Select all
Player = {}
function Player:new()
o = {
x = 30,
y = 30,
w = 20,
h = 20,
speed = 100,
score = 0,
}
setmetatable(o, {__index = player})
return o
end
function Player:update(dt)
if love.keyboard.isDown("lshift") then -- this is the sprint line
o.speed = 300
end
if love.keyboard.isDown("a") or love.keyboard.isDown("right") then
o.x = o.x + o.speed * dt
end
if love.keyboard.isDown("d") or love.keyboard.isDown("left") then
o.x = o.x - o.speed * dt
end
if love.keyboard.isDown("w") or love.keyboard.isDown("up") then
o.y = o.y - o.speed * dt
end
if love.keyboard.isDown("s") or love.keyboard.isDown("down") then
o.y = o.y + o.speed * dt
end
if o.x > love.graphics.getWidth() - o.w then
o.x = love.graphics.getWidth() - o.w
end
if o.y > love.graphics.getHeight() - o.h then
o.y = love.graphics.getHeight() - o.h
end
if o.x < 0 then
o.x = 0
end
if o.y < 0 then
o.y = 0
end
end
function Player:draw()
love.graphics.setColor(255, 0, 0)
love.graphics.rectangle("fill", o.x, o.y, o.w, o.h)
end
I will post a love file if needed