Need help creating random images from an index

I’m trying to determine how to update this function to randomly generate different images to spawn. I’ve tried numerous ways that I can think of… and have gotten it to rotate through the different images however I’m still confused as everything I’ve found doesn’t match this use.

It generates different images to a grid, and fills the grid with an image after x timer. I tried setting a math.random / math.randomseem variable but somehow it didn’t work correctly still. I also tried a function within the function however it returns more than 60 chars so it errors out…

Hopefully that’s enough info for someone to help me?

Let me know if not.

Current:

local function bunnyAnimation()
    for i, obj in pairs(bunny) do
        if bunny[i].hit < bunny[i].hit_q then bunny[i].timer = bunny[i].timer + tDelta
        else bunny[i].timer = bunny[i].timer - tDelta end
        if bunny[i].timer < 0 then
            killObj(bunny[i])
        end

        step = 30
        
        if bunny[i].timer >= 0 and bunny[i].timer < step then bunny[i].fill = {type=“image”, filename=“Images/base/bunny1.png”}
        elseif bunny[i].timer >= step and bunny[i].timer < 2*step then bunny[i].fill = {type=“image”, filename=“Images/base/bunny2.png”}
        elseif bunny[i].timer >= 2*step and bunny[i].timer < 3*step then bunny[i].fill = {type=“image”, filename=“Images/base/bunny3.png”}
        elseif bunny[i].timer >= 3*step then bunny[i].fill = {type=“image”, filename=“Images/base/bunny4.png”}
        end

        if bunny[i].timer>=1000 then
            bunny[i].hit = 5
            bunny[i].status = “idle”
            bunny[i].timer = 3*step
        end
    end
end

I will try to help if I can.

check out this post earlier in the week and the response(help) posted by roaminggamer :

https://forums.coronalabs.com/topic/68104-removing-an-object-from-group-throwing-up-error/

I think it is relevant, but not maybe exactly like your issue, but similar enough it should be helpful to you regarding the error you are getting.

I might be wrong, but it appears to me you are using a timer, to flip thru 4 images of a bunny to create an animation; but I really just briefed over the code quickly…  In any case, corona has an image sheet that does all that for you and much more effectively and efficiently … check out this link:

https://docs.coronalabs.com/api/library/graphics/newImageSheet.html

just a suggestion… the best i can tell from your post … even if you are not doing a simple animation and are just picking and swapping various images to randomly fill in grid cells, use of the imagesheets would work better in such a situation.  Once you check the imageSheet api out, you may find it most helpful in many way.s

any further post you may make it really helps to use the ‘<>’ on the toolbar when posting sample code. It makes it easier to read

Hope this helps and Good Luck

Bob

Thanks for your reply. I ended up figuring out after further analyzing the code more and determined I should just duplicate that function multiple times with different images for each displayed, then in the main code section have it reference the new named multiples functions and have if random ==1 then function1… if random ==2, then function 2, etc, etc.

I previously had gotten it to randomly rotate to cycle within all of the pics within the one function but it was for one animation, not generating one pic, going away, then a random other pic, etc as I wanted.

I had seen the newimagesheet items you mentioned but couldn’t get the formatting to work out right with what the timer/grid items were doing in the code so I left it as is.

I’m good now.

Thanks for the tip about the <> code button.

It is really, really, REALLY, REALLY bad programming to duplicate functions that have only minor changes.

What you need is sprites.  There are a couple of project samples that use sprites to perform animations.

On a separate note,  killObj ( bunny [i]) just sounds real harsh - depending on how you read it!

I will try to help if I can.

check out this post earlier in the week and the response(help) posted by roaminggamer :

https://forums.coronalabs.com/topic/68104-removing-an-object-from-group-throwing-up-error/

I think it is relevant, but not maybe exactly like your issue, but similar enough it should be helpful to you regarding the error you are getting.

I might be wrong, but it appears to me you are using a timer, to flip thru 4 images of a bunny to create an animation; but I really just briefed over the code quickly…  In any case, corona has an image sheet that does all that for you and much more effectively and efficiently … check out this link:

https://docs.coronalabs.com/api/library/graphics/newImageSheet.html

just a suggestion… the best i can tell from your post … even if you are not doing a simple animation and are just picking and swapping various images to randomly fill in grid cells, use of the imagesheets would work better in such a situation.  Once you check the imageSheet api out, you may find it most helpful in many way.s

any further post you may make it really helps to use the ‘<>’ on the toolbar when posting sample code. It makes it easier to read

Hope this helps and Good Luck

Bob

Thanks for your reply. I ended up figuring out after further analyzing the code more and determined I should just duplicate that function multiple times with different images for each displayed, then in the main code section have it reference the new named multiples functions and have if random ==1 then function1… if random ==2, then function 2, etc, etc.

I previously had gotten it to randomly rotate to cycle within all of the pics within the one function but it was for one animation, not generating one pic, going away, then a random other pic, etc as I wanted.

I had seen the newimagesheet items you mentioned but couldn’t get the formatting to work out right with what the timer/grid items were doing in the code so I left it as is.

I’m good now.

Thanks for the tip about the <> code button.

It is really, really, REALLY, REALLY bad programming to duplicate functions that have only minor changes.

What you need is sprites.  There are a couple of project samples that use sprites to perform animations.

On a separate note,  killObj ( bunny [i]) just sounds real harsh - depending on how you read it!