Picking coins and removing them when off screen

Hey guys, so i have this platform game where char is flying and picking coins. Something very basic, now i have this function that creates coins:

    – Table

    local zlatnici = {}

    – funkcija for spawn

     function spawnzlatnik(event)

        zlatnik = display.newImage(“coin.png”)  

        zlatnik.x =  _W–math.random(40, _W - 80 )  

        zlatnik.y =  math.random(10, _H - 70 )

        zlatnik.Name = “zlatnik”

        physics.addBody( zlatnik, ‘static’, {density=3, friction=0.5, bounce=0.3 } ) 

        zlatnik.index = #zlatnici + 1 

        zlatnici[zlatnik.index] = zlatnik –

        group:insert(zlatnik)  

        return zlatnik

    end

    – Timer for spawn

    tmrSpawn = timer.performWithDelay(500, spawnzlatnik, 0)

Its working as it should, its creating coins and adding them to table, but if they go off screen i want them removed from table, so i have this:

local function brisizlatnika()

        for i, v in pairs(zlatnici) do

            if zlatnici[i].x < -_W then

                zlatnici[i]:removeSelf()

                table.remove(zlatnici, i)

            else 

                zlatnici[i].x =zlatnici[i].x - 6

            end

        end

end

Runtime:addEventListener( “enterFrame”, brisizlatnika )

As u can see, its checking for each coin in table if its off the screen and removing it, BUT the issue comes when player picks up the coin:

local function onCollision( event )

    if ( event.phase == “began” ) then

        audio.play(sndKill)

        if(event.object1.Name == “zlatnik” and event.object2.Name == “ptica”) then

            event.object1:removeSelf();

            print( event.object1.index )

            table.remove(zlatnici, event.object1.index)

        end

    end

end

Runtime:addEventListener( “collision”, onCollision )

Its removing itself, its removing itself from table, but the issue is that first function that is deleting coins off screen is looping from i to v (I SUPPOSE, as i am more of front end dev), but when i delete it during collision, its deleting it from table and in this loop we dont have anymore that particular i so its giving me error for that function, correct ?

What would be the CORRECT way to do this ?

Many many thanks, as i just started working with corona and want to do it the right way

ok ,for now i found a way around by just setting event.object1.alpha = 0 on collision and letting the first function remove it once it goes offscreen. But i am sure there is better way to do this

That’s the same way that I implement coin collisions. I think it’s probably the simplest and safest way of controlling the mechanic.

ok ,for now i found a way around by just setting event.object1.alpha = 0 on collision and letting the first function remove it once it goes offscreen. But i am sure there is better way to do this

That’s the same way that I implement coin collisions. I think it’s probably the simplest and safest way of controlling the mechanic.