Moving Sprites Cross Screen?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Lua_Beginner
Prole
Posts: 4
Joined: Sat Apr 19, 2025 10:54 pm

Moving Sprites Cross Screen?

Post by Lua_Beginner »

Hello all,

I have start to learning LUA and start doing basic LUA like variable, text on screen, Sprites on screen and on.

I was trying to do moving sprites to try move sprite cross the screen but it not working for some reason.

local x = 0

function love.load()
-- Everything will be loaded here
Beast=love.graphics.newImage("Beast.png")
end


function love.update(dt)
-- Everything will be updated here
end


function love.draw(
-- Everything will be drawn here
for x=1, 1, 10 do
love.graphics.draw(Beast,x,10)
end

end

I was trying to do for loop to move sprites cross the screen and I guess for loop in update..........right?
tauli
Prole
Posts: 1
Joined: Mon Apr 21, 2025 5:07 pm

Re: Moving Sprites Cross Screen?

Post by tauli »

You don't need a for loop to update the position of the sprite. love.draw() is called by the engine every time a new frame is drawn (ideally ~60 times per second) and love.update(dt) is called by the engine every time the game state should be updated, where dt is the time in seconds that passed since love.update was last called. You can just add dt to x inside of love.update and maybe multiply dt by a factor to make the movement faster or slower and simply draw the sprite inside of love.draw at the current position.
User avatar
dusoft
Party member
Posts: 792
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Moving Sprites Cross Screen?

Post by dusoft »

Use dt delta time to change the position, position change of x in loop won't be visible as it happens instantly.

https://love2d.org/wiki/dt
Lua_Beginner
Prole
Posts: 4
Joined: Sat Apr 19, 2025 10:54 pm

Re: Moving Sprites Cross Screen?

Post by Lua_Beginner »

thanks for helping me but I get error which show code blow

Code: Select all

local x = 0

function love.load()
  -- Everything will be loaded here
Beast=love.graphics.newImage("Beast.png")
end


function love.update(dt)
  -- Everything will be updated here
x=x*dt
end


function love.draw(
  -- Everything will be drawn here 
      love.graphics.draw(Beast,x,10) -- Why Error?
end
Last edited by Lua_Beginner on Tue Apr 22, 2025 6:48 pm, edited 1 time in total.
User avatar
dusoft
Party member
Posts: 792
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Moving Sprites Cross Screen?

Post by dusoft »

Please, fix your posting to use full editor/preview and

Code: Select all

code
\[code\] formatting.
Lua_Beginner
Prole
Posts: 4
Joined: Sat Apr 19, 2025 10:54 pm

Re: Moving Sprites Cross Screen?

Post by Lua_Beginner »

It is ok as I got it working.

Code: Select all

local x = 0

function love.load()
  -- Everything will be loaded here
Beast=love.graphics.newImage("Beast.png")
end


function love.update(dt)
  -- Everything will be updated here
x=x+1-dt
end


function love.draw()
  -- Everything will be drawn here 
      love.graphics.draw(Beast,x,100) 
end
User avatar
dusoft
Party member
Posts: 792
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Moving Sprites Cross Screen?

Post by dusoft »

Great! Good luck.
RNavega
Party member
Posts: 465
Joined: Sun Aug 16, 2020 1:28 pm

Re: Moving Sprites Cross Screen?

Post by RNavega »

It's painful to learn by trial and error like that if you've never used Lua or programmed some games before.
I think it'll speed up your training if you follow a tutorial so you can learn by example.

Here are some cool resources, stolen borrowed from a Reddit thread:

Lua CheatSheet:
https://tylerneylon.com/a/learn-lua/

LÖVE tutorial series (remember you can speed up the video in the player settings, like using 1.5x speed etc):
https://www.youtube.com/playlist?list=P ... qya9Iiefm9

The classic Sheepolution course:
https://sheepolution.com/learn/book/contents
Lua_Beginner
Prole
Posts: 4
Joined: Sat Apr 19, 2025 10:54 pm

Re: Moving Sprites Cross Screen?

Post by Lua_Beginner »

Thanks Dusoft for wishing me good luck :)

Thanks RNavega for those interesting tutorials, it gong take bit of time to get used Lua Programming Language and see where it take me from there.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 6 guests