unable to move image
Posted: Sat Jan 05, 2019 9:26 am
i'm fairly new to lua scripting and need help with this script:
there are two errors: 1. when I press the "d" key, the player (an image) plays a walking animation but doesn't move (x + (speed * dt).
2. when I press the "a" key, the player doesn't do anything.
does anyone know how to fix this problem and explain why it's happening?
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
2. when I press the "a" key, the player doesn't do anything.
does anyone know how to fix this problem and explain why it's happening?