i cant get my image to move with my arrow keys

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
justthoseguys12
Prole
Posts: 13
Joined: Fri Nov 22, 2024 7:18 pm

i cant get my image to move with my arrow keys

Post 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
Attachments
main.love
(3.57 KiB) Downloaded 170 times
:neko: :neko: :neko: :neko: :neko: :neko: :neko: :neko: :neko: :neko: :neko: :3 :neko: :neko: :neko: :neko: :neko: :neko: :neko: :3 :neko: :neko: :neko: :neko: :neko: :neko: :neko: :neko: :neko:
User avatar
marclurr
Party member
Posts: 157
Joined: Fri Apr 22, 2022 9:25 am

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

Post by marclurr »

Can you show us the error?
User avatar
justthoseguys12
Prole
Posts: 13
Joined: Fri Nov 22, 2024 7:18 pm

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

Post 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'
:neko: :neko: :neko: :neko: :neko: :neko: :neko: :neko: :neko: :neko: :neko: :3 :neko: :neko: :neko: :neko: :neko: :neko: :neko: :3 :neko: :neko: :neko: :neko: :neko: :neko: :neko: :neko: :neko:
User avatar
dusoft
Party member
Posts: 654
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

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

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests