removing a table item

Hi,

I am struggling here, I have 2 modules, player.lus, obsticle.lua.

I have gems created in the obsticle.lua and the player alond with the collision function in the player.lua.

The collisions are working fine but I am unable to remove the gem cleanly.

If I use event.other:removeSelf() it will remove the gem but I get an error in the function I use to move them.

I know I have to remove the gem from the table but I just cant it to work…

The player module dosnt see the gem table and if I pu the removal function in the obsticle.lua where i create them i cant pass event other.

any advcie would be cool:

[lua]

        
        

-----------------obsticles.lua----------------

function createObsticles()

gems = {}

gems[1] = display.newImage(layer,“images/gem1.png”)

       gems[1].x = 50; gems[1].y = display.contentHeight /2 -30

       gems[1].dx = speed

       physics.addBody(gems[1],“static”)

       gems[1].type = “gems”

…gems[2], gems[3] ,etc etc, etc …

 local tPrevious = system.getTimer()

       local function move2(event)

           local tDelta = event.time - tPrevious

           tPrevious = event.time

           local g

           for g = 1, #gems, 1 do

               gems[g].x = gems[g].x - gems[g].dx * tDelta * 0.2

               if (gems[g].x + gems[g].contentWidth) < 0 then

                   gems[g]:translate( 1080 + gems[g].contentWidth * 2, 0 )

               end

           end

       end

Runtime:addEventListener( “enterFrame”, move2 )

end

------------------------------------player.lua------------------------------

local player

function createPlayer()

player= display…blah blah blah

 function player:collision(event)

        local phase = event.phase

        if event.phase == “began” then

           

                event.target.type == “player” and event.other.type == “gems” then

                print("…gem collision")

           

    end

end

end

        [/lua]

when you setup your gems save it’s table index like gems[1].index = 1

on collision before you do removeSelf call gems[event.other.index] = nil

this however will break your for loop in the move2 function so change the for loop to

for i, g in pairs(gems) do

and g is your gem so change all the gems[g] to just g.

@primoz,

thank you for your kind help, I tried this out and it works beautifully.

Plus, thanks for explaining the code you said try, that is most helpful in understanding whats going on.

:slight_smile:

when you setup your gems save it’s table index like gems[1].index = 1

on collision before you do removeSelf call gems[event.other.index] = nil

this however will break your for loop in the move2 function so change the for loop to

for i, g in pairs(gems) do

and g is your gem so change all the gems[g] to just g.

@primoz,

thank you for your kind help, I tried this out and it works beautifully.

Plus, thanks for explaining the code you said try, that is most helpful in understanding whats going on.

:slight_smile: