Removing Image Object With Physics From A Table And Replacing It With New One

I am very new to this and have spent hours trying to figure it out…

Basically I have a board with 5 balls on it with 5 different numbered values. Once a ball is hit by the player it will disappear.

All I want to do is replace the ball in the table with another one. So for example if ball number 4 is destroyed I have a loop to check what ball is destroyed and to replace it.

Initially I have a loop which inserts 5 balls and there relevant values like this then adds the physics properties to it.

[lua] 

table.insert(EnemyBallsTable, display.newImage(BallTable[RandomZ].BallFile, RandomX, RandomY))

EnemyBallsTable[i].Value = RandomZ

EnemyBallsTable[i].TablePosition = i

EnemyBallsTable[i].IsAlive = 1

physics.addBody( EnemyBallsTable[i] , “static”, { density = 1.0, friction = 0.3, bounce = 0.2 } )

[/lua] 

Once the player collides with the ball then this gets rid of it…

[lua] 

local function Oncollision( event )

   

    if event.object1.Value == ANSWER and event.object2.myName == “Player” then

           TheSum.text = (“CORRECT”)

           SumSolved = 0

    elseif event.object1.Value ~= ANSWER and event.object2.myName == “Player” and event.object1.Value ~= “X” then

            TheSum.text = (“WRONG”)

           SumSolved = 0

            print("Table Position Destroyed = " … event.object1.TablePosition)

            EnemyBallsTable[event.object1.TablePosition]:removeSelf()

            --EnemyBallsTable[event.object1.TablePosition].IsAlive = 0

            EnemyBallsTable[event.object1.TablePosition] = nil

            --table.remove(EnemyBallsTable[event.object1.TablePosition])

            elseif event.object1.Value ~= ANSWER and event.object2.myName == “Player” and event.object1.Value == “X” then

    end

end

[/lua] 

Every 5 seconds a ball is generated either in place of one thats been destroyed OR added on to the end of the table…

[lua]function GenerateNewBalls ()

    --print (“Generating New Balls”)

    --print ("Table Position 1 = " … EnemyBallsTable[1].IsAlive)

    --print ("Table Position 2 = " … EnemyBallsTable[2].IsAlive)

    --print ("Table Position 3 = " … EnemyBallsTable[3].IsAlive)

    --print ("Table Position 4 = " … EnemyBallsTable[4].IsAlive)

    --print ("Table Position 5 = " … EnemyBallsTable[5].IsAlive)

    --print ("Table Position 6 = " … EnemyBallsTable[1].IsAlive)

    --print ("Table Position 7 = " … EnemyBallsTable[2].IsAlive)

    --print ("Table Position 8 = " … EnemyBallsTable[3].IsAlive)

    --print ("Table Position 9 = " … EnemyBallsTable[4].IsAlive)

    --print ("Table Position 10 = " … EnemyBallsTable[5].IsAlive)

     RandomZ = math.random(5)

     RandomX = math.random(25, 560)

     RandomY = math.random(25, 440)

     AddedEnemyCheck = 0

     for i=1,#EnemyBallsTable do

         --Check if space availible in the table

         if EnemyBallsTable[i] == nil and AddedEnemyCheck == 0 then

           print("Check 1 — i = " … i)

         table.insert(EnemyBallsTable[i], display.newImage(BallTable[RandomZ].BallFile, RandomX, RandomY))

         physics.addBody(EnemyBallsTable[i], “static”, { density = 1.0, friction = 0.3, bounce = 0.2 } )

         EnemyBallsTable[i].Value = RandomZ

         EnemyBallsTable[i].TablePosition = i

         EnemyBallsTable[i].IsAlive = 1

             AddedEnemyCheck = 1

         elseif EnemyBallsTable[i].IsAlive == 1 and AddedEnemyCheck == 0 then

             --print("Cannot Add New Enemy At Postion " … EnemyBallsTable[i])

               print(“Check 2”)

         --else if i == #EnemyBallsTable and AddedEnemyCheck = 0 then

          –  table.insert(EnemyBallsTable[i], display.newImage(BallTable[RandomZ].BallFile, RandomX, RandomY))

          –  EnemyBallsTable[i].Value = RandomZ

          –  EnemyBallsTable[i].TablePosition = i

          –  EnemyBallsTable[i].IsAlive = 1

         end

         if i == #EnemyBallsTable and AddedEnemyCheck == 0 then

             i = i + 1

             print("Insert Table = "… i)

             table.insert(EnemyBallsTable, display.newImage(BallTable[RandomZ].BallFile, RandomX, RandomY))

             EnemyBallsTable[i].Value = RandomZ

             EnemyBallsTable[i].TablePosition = i

             EnemyBallsTable[i].IsAlive = 1

             physics.addBody( EnemyBallsTable[i] , “static”, { density = 1.0, friction = 0.3, bounce = 0.2 } )

         end

         --EnemyBallsTable[i].IsAlive = True

     end

[/lua] 

Now it adds to the end of the table but will not replace one thats missing. 

Is this a sensible way of doing it or should I just add to the end of the table all the time and just remove the image of the destoyed ball, set its alive variable = 0 and carry on like that. The trouble with this is that the table could go up to 100+ and Im not sure that is a sensible way to do it!!!

Sorry if my code sucks but this is my first attempt at programming since college 16 years ago…

Appreciate any help with this!!!

:slight_smile: