i cant get my image to move with my arrow keys
Posted: Tue Dec 03, 2024 3:00 pm
so i have it so when i press up, down etc... using love.graphics.translate it should move my spriteforward variable one pixel but instead it crashes whenever i press the keys here is the code and the file
Code: Select all
function love.load()
spriteforward = love.graphics.newImage("sprite 4.png")
spritebackward = love.graphics.newImage("sprite 2.png")
spriteleft = love.graphics.newImage("sprite 1.png")
spriteright = love.graphics.newImage("sprite 3.png")
love.window.setFullscreen(true, "desktop")
end
function love.draw()
love.graphics.draw(spriteforward, 683, 384)
end
function love.update(dt)
local speed = 100
local dx, dy = 0,0
local isScancodeDown = love.keyboard.isScancodeDown
if isScancodeDown ('a') then dx = dx - dt*speed end
if isScancodeDown ('d') then dx = dx + dt*speed end
if isScancodeDown ('w') then dy = dy - dt*speed end
if isScancodeDown ('s') then dy = dy + dt*speed end
end
function love.keypressed(key, scancode, isrepeat)
if key == "escape" then
love.event.quit()
end
if key == "w" then
love.event.push(up)
end
if key == "s" then
love.event.push(down)
end
if key == "a" then
love.event.push(left)
end
if key == "d" then
love.event.push(right)
end
if key == "up" then
love.graphics.translate(spriteforward, 0, -1)
end
if key == "down" then
love.graphics.translate(spriteforward, 0, 1)
end
if key == "left" then
love.graphics.translate(spriteforward, -1, 0)
end
if key == "right" then
love.graphics.translate(spriteforward, 1, 0)
end
end