Page 1 of 1

i cant get my image to move with my arrow keys

Posted: Tue Dec 03, 2024 3:00 pm
by justthoseguys12
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

Re: i cant get my image to move with my arrow keys

Posted: Tue Dec 03, 2024 5:42 pm
by marclurr
Can you show us the error?

Re: i cant get my image to move with my arrow keys

Posted: Tue Dec 03, 2024 7:10 pm
by justthoseguys12

Code: Select all

Error

main.lua:50: bad argument #1 to 'translate' (number expected, got userdata)


Traceback

[love "callbacks.lua"]:228: in function 'handler'
[C]: in function 'translate'
main.lua:50: in function <main.lua:24>
[love "callbacks.lua"]:154: in function <[love "callbacks.lua"]:144>
[C]: in function 'xpcall'

Re: i cant get my image to move with my arrow keys

Posted: Wed Dec 04, 2024 11:20 am
by dusoft
You need to follow the docs and function signatures and not include random copied/pasted params:
love.graphics.translate( dx, dy )
https://love2d.org/wiki/love.graphics.translate