How to make function for images?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
kostavanga
Prole
Posts: 3
Joined: Thu Aug 24, 2023 4:29 pm

How to make function for images?

Post by kostavanga »

I am new to programming. Therefore, I can't code a function that .draw images automatically when they are in the folder.

Code: Select all

function maker()
    AllImgFiles = love.filesystem.getDirectoryItems(dir)
    local n = 1
    while n <= (#AllImgFiles) do    
    table.insert(newdir, dir .. AllImgFiles[n])
    table.insert(img, love.graphics.newImage(newdir[n]))
    table.insert(imgWidth, img[n]:getWidth() +10) 
    n = n + 1
    end
end
I made this function that automatically detects images and makes a direction for them( this part is working)

Code: Select all

 
_G.times = 1    
function images(s, x, y )
    for i = 1, (#AllImgFiles), 1 do
        love.graphics.draw(img[times],x, y, 0, s, s, tiw[times])
        times = times + 1 
    end
end    
function autoimg(s, x, y )
    if times < (#AllImgFiles) then 
        images(s, x, y)
    end
end
I got this for automatic .draw of these images, but they are showing for 1 frame and then disappear. Autoimg is in love.draw() and maker() in love.load()
User avatar
dusoft
Party member
Posts: 510
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: How to make function for images?

Post by dusoft »

You will need a tweening library or an animation library to properly loop over all frames.
Also you might need to reset timer (times variable) when loop finishes.
User avatar
GVovkiv
Party member
Posts: 670
Joined: Fri Jan 15, 2021 7:29 am

Re: How to make function for images?

Post by GVovkiv »

dusoft wrote: Thu Aug 24, 2023 9:11 pm You will need a tweening library or an animation library to properly loop over all frames.
Also you might need to reset timer (times variable) when loop finishes.
I don't think this is about animations. I guess they want to load all images from some directory into table and then via loop through all images in table and draw them.
kostavanga
Prole
Posts: 3
Joined: Thu Aug 24, 2023 4:29 pm

Re: How to make function for images?

Post by kostavanga »

GVovkiv wrote: Thu Aug 24, 2023 10:28 pm
dusoft wrote: Thu Aug 24, 2023 9:11 pm You will need a tweening library or an animation library to properly loop over all frames.
Also you might need to reset timer (times variable) when loop finishes.
I don't think this is about animations. I guess they want to load all images from some directory into table and then via loop through all images in table and draw them.
Thank you for your answers. That's what I was trying to do. However, when I used loop image disappeared when loop ended. I don't know how to make my function run for certain times and not end. I also tried to use a while loop, but after a minute all my RAM was used.
User avatar
BrotSagtMist
Party member
Posts: 614
Joined: Fri Aug 06, 2021 10:30 pm

Re: How to make function for images?

Post by BrotSagtMist »

I dont see why you created the variable _times_ here.
Without this, the code should just work.
obey
kostavanga
Prole
Posts: 3
Joined: Thu Aug 24, 2023 4:29 pm

Re: How to make function for images?

Post by kostavanga »

BrotSagtMist wrote: Thu Aug 24, 2023 11:41 pm I dont see why you created the variable _times_ here.
Without this, the code should just work.
Thank you very much for your answer! It is working!!!!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 64 guests