Page 1 of 1

How to make function for images?

Posted: Thu Aug 24, 2023 4:48 pm
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()

Re: How to make function for images?

Posted: Thu Aug 24, 2023 9:11 pm
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.

Re: How to make function for images?

Posted: Thu Aug 24, 2023 10:28 pm
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.

Re: How to make function for images?

Posted: Thu Aug 24, 2023 11:10 pm
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.

Re: How to make function for images?

Posted: Thu Aug 24, 2023 11:41 pm
by BrotSagtMist
I dont see why you created the variable _times_ here.
Without this, the code should just work.

Re: How to make function for images?

Posted: Thu Aug 24, 2023 11:56 pm
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!!!!