The problem is that in love.update almost all of your ifs are nested, which is probably not what you want. It's a common mistake, I did it too. Just remember
the old saying in Lua.
Here's how your code looks like if we put tabs after an if:
Code: Select all
function love.load()
DtimePassed = 0
AtimePassed = 0
right = love.graphics.newImage("sprites/right.png")
right2 = love.graphics.newImage("sprites/right2.png")
left = love.graphics.newImage("sprites/left.png")
left2 = love.graphics.newImage("sprites/left2.png")
player = right
x = 250
y = 250
speed = 300
end
function love.draw()
love.graphics.draw(player, 250, 250)
love.graphics.print("DtimePassed:"..tostring(DtimePassed), 0, 0)
love.graphics.print("AtimePassed:"..tostring(AtimePassed), 250, 0)
end
function love.update(dt)
if love.keyboard.isDown("d") then
x = x + speed
DtimePassed = DtimePassed + 1
if DtimePassed > 0 then
player = right2
if DtimePassed > 20 then
player = right
if DtimePassed > 50 then
DtimePassed = 0
end
if love.keyboard.isDown("a") then
x = x - (speed * dt)
AtimePassed = AtimePassed + 1
if AtimePassed > 0 then
player = left2
if AtimePassed > 20 then
player = right
if AtimePassed > 50 then
DtimePassed = 0
end
end
end
end
end
end
end
function love.keyreleased(key)
if key == "d" then
player = right
DtimePassed = 0
if key == "a" then
player = left
AtimePassed = 0
end
end
end
end
From here we can see why your errors are happening:
First of all, in love.draw(), when drawing the player sprite you did
love.graphics.draw(player, 250, 250), notice that you are saying "draw the player in the coordinates (250, 250).", so the player won't move from those numbers. To fix it you have to pass the x and y variables to love.graphics.draw so löve knows where to draw the player when moving, so you have to write
love.graphics.draw(player, x, y)
Nothing happens when pressing "a" because love.keyboard.isDown("a") is reachable only if you previously pressed "d", so in order to fix that we need to rearrange the ifs.
Code: Select all
function love.load()
DtimePassed = 0
AtimePassed = 0
right = love.graphics.newImage("sprites/right.png")
right2 = love.graphics.newImage("sprites/right2.png")
left = love.graphics.newImage("sprites/left.png")
left2 = love.graphics.newImage("sprites/left2.png")
player = right
x = 250
y = 250
speed = 300
end
function love.draw()
love.graphics.draw(player, x, y) -- x, y instead of 250, 250
love.graphics.print("DtimePassed:"..tostring(DtimePassed), 0, 0)
love.graphics.print("AtimePassed:"..tostring(AtimePassed), 250, 0)
end
function love.update(dt)
if love.keyboard.isDown("d") then -- Pressing d?
x = x + speed
DtimePassed = DtimePassed + dt
if DtimePassed > 0 and DtimePassed < 20 then -- Use right2 the first 20 frames
player = right2
elseif DtimePassed >= 20 and DtimePassed < 50 then -- Use right the next 30 frames
player = right
elseif DtimePassed >= 50 then -- Reset when reaching 50 frames
DtimePassed = 0
end
elseif love.keyboard.isDown("a") then -- Player is not pressing d. Is he pressing a?
x = x - (speed * dt)
AtimePassed = AtimePassed + 1
if AtimePassed > 0 and AtimePassed < 20 then -- Use the same logic
player = left2
elseif AtimePassed >= 20 then
player = right
elseif AtimePassed >= 50 then
DtimePassed = 0
end
end
end
function love.keyreleased(key)
if key == "d" then
player = right
DtimePassed = 0
elseif key == "a" then
player = left
AtimePassed = 0
end
end
Notice how this beautiful code is easier to read, and it does the movement you want it to do!
However, it still have a few quirks. When pressing "d" the player moves to the right really fast, this is because you forgot to multiply by dt, so instead of
x = x + speed, do
x = x + (speed * dt).
The last thing I notice is that then moving to the left, when AtimePassed > 0 you use the "left2" sprite, but when AtimePassed > 20 you use the "right" sprite. I assume you mean to use the "left" sprite. And when AtimePassed > 50 you set DtimePassed to 0, it should be AtimePassed.
So the final code would be like this:
Code: Select all
function love.load()
DtimePassed = 0
AtimePassed = 0
right = love.graphics.newImage("sprites/right.png")
right2 = love.graphics.newImage("sprites/right2.png")
left = love.graphics.newImage("sprites/left.png")
left2 = love.graphics.newImage("sprites/left2.png")
player = right
x = 250
y = 250
speed = 300
end
function love.draw()
love.graphics.draw(player, x, y)
love.graphics.print("DtimePassed:"..tostring(DtimePassed), 0, 0)
love.graphics.print("AtimePassed:"..tostring(AtimePassed), 250, 0)
end
function love.update(dt)
if love.keyboard.isDown("d") then -- Pressing d?
x = x + (speed * dt)
DtimePassed = DtimePassed + 1
if DtimePassed > 0 and DtimePassed < 20 then -- Use right2 the first 20 frames
player = right2
elseif DtimePassed >= 20 and DtimePassed < 50 then -- Use right the next 30 frames
player = right
elseif DtimePassed >= 50 then -- Reset when reaching 50 frames
DtimePassed = 0
end
elseif love.keyboard.isDown("a") then -- Player is not pressing d. Is he pressing a?
x = x - (speed * dt)
AtimePassed = AtimePassed + 1
if AtimePassed > 0 and AtimePassed < 20 then -- Use the same logic
player = left2
elseif AtimePassed >= 20 and AtimePassed < 50 then
player = left
elseif AtimePassed >= 50 then
AtimePassed = 0 -- not DtimePassed since we are moving to the left!
end
end
end
function love.keyreleased(key)
if key == "d" then
player = right
DtimePassed = 0
elseif key == "a" then
player = left
AtimePassed = 0
end
end
I don't know if you're following any tutorial or you're just playing around by yourself, but if you want a good tutorial that explains the basics of Lua and löve2d check
this one.