problem with array and for loop

greetings people,
im trying to make a memery game, so i found an example on it and made some adjustments.
the problem is i dont really 100% understand how it works thus it gives me some problems when trying to remove things from the scene

setting up the game goes as follows:
 

for count = 1,4 do x = x + 100 y = -50 for insideCount = 1,5 do y = y + 150 --Assign each image a random location on grid temp = math.random(1,#buttonImages) button[count] = display.newImage(buttonImages[temp] .. ".png"); --Position the button button[count].x = x; button[count].y = y; --Give each a button a name button[count].myName = buttonImages[temp] button[count].number = totalButtons button[count].id=buttonid[#buttonImages] --Remove button from buttonImages table table.remove(buttonImages, temp) buttonCover[totalButtons] = display.newImage("button.png"); buttonCover[totalButtons].x = x; buttonCover[totalButtons].y = y; buttonCover[totalButtons].width=100 buttonCover[totalButtons].height=100 totalButtons = totalButtons + 1 button[count].touch = game button[count]:addEventListener( "touch", button[count] ) button[count].width=100 button[count].height=100 end end

removing the buttonCover goes easy enough but its the removal of the buttons that gives me struggle.

i try to remove both this way

 

local function handleButtonEvent2( event ) if ( "ended" == event.phase ) then if (event.target.id == "backbtn") then button5.alpha=0 for lul=1,4 do button[lul]:removeSelf( ) button[lul]=nil end for lal=0,20 do if (buttonCover[lal]==nil) then print("buttonCover"..lal.. "was nil") else buttonCover[lal]:removeSelf( ) buttonCover[lal]=nil end end table.remove( buttonCover) table.remove( button) end end end

my question is, how to i remove all 20 buttons as there is only button[count] to 4
is there 5 buttons inside each button[count]??
if so how to i remove it? if not how is it then stored?

You need to set up a two-dimensional array, at the moment you are creating 5 buttons, all with the same reference of button[count]. This reference will only point to the last button created and you have no way to access the others.

[lua]

local button = {}

for count = 1 , 4 do

      x = x + 100

      y = - 50

      button[count] = {}      – create a table within a table (two-dimensional array)

     

      for insideCount = 1 , 5 do

            y = y + 150

           

            --Assign each image a random location on grid

            temp = math.random( 1 ,#buttonImages)

     

            local i = display.newImage(buttonImages[temp] ".png");   – using ‘i’ is easier than

            – constantly refering to button[count][insideCount], we’ll assign it later  

           

            --Position the button

            i.x = x;

            i.y = y;         

           

            --Give each a button a name

            i.myName = buttonImages[temp]

            i.number = totalButtons

            i.id=buttonid[#buttonImages]

            i.touch = game         

            i:addEventListener( “touch”, i )

            i.width=100

            i.height=100

            button[count][insideCount] = i      – put the button image in two-dimensional array

           

            --Remove button from buttonImages table

            table.remove(buttonImages, temp)

                       

            buttonCover[totalButtons] = display.newImage(“button.png”);

            buttonCover[totalButtons].x = x; buttonCover[totalButtons].y = y;

            buttonCover[totalButtons].width=100

            buttonCover[totalButtons].height=100

            totalButtons = totalButtons + 1

           

      end

end

[/lua]

Alternatively, to make it easier to remove the buttons later, put them in a one-dimensional array like this:

[lua]

local button = {}

for count = 1 , 4 do

      x = x + 100

      y = - 50

     

      for insideCount = 1 , 5 do

            y = y + 150

           

            --Assign each image a random location on grid

            temp = math.random( 1 ,#buttonImages)

     

            local i = display.newImage(buttonImages[temp] ".png");   – using ‘i’ is easier than

            – constantly refering to button[count][insideCount], we’ll assign it later  

           

            --Position the button

            i.x = x;

            i.y = y;         

           

            --Give each a button a name

            i.myName = buttonImages[temp]

            i.number = totalButtons

            i.id=buttonid[#buttonImages]

            i.touch = game         

            i:addEventListener( “touch”, i )

            i.width=100

            i.height=100

            button[#button+1] = i   – put the button image in one-dimensional array at next available slot

           

            --Remove button from buttonImages table

            table.remove(buttonImages, temp)

                       

            buttonCover[#button] = display.newImage(“button.png”);

            buttonCover[#button].x = x; buttonCover[totalButtons].y = y;

            buttonCover[#button].width=100

            buttonCover[#button].height=100

      end

end

[/lua]

thanks, puttin them in a one-dimensional array worked for me.

You need to set up a two-dimensional array, at the moment you are creating 5 buttons, all with the same reference of button[count]. This reference will only point to the last button created and you have no way to access the others.

[lua]

local button = {}

for count = 1 , 4 do

      x = x + 100

      y = - 50

      button[count] = {}      – create a table within a table (two-dimensional array)

     

      for insideCount = 1 , 5 do

            y = y + 150

           

            --Assign each image a random location on grid

            temp = math.random( 1 ,#buttonImages)

     

            local i = display.newImage(buttonImages[temp] ".png");   – using ‘i’ is easier than

            – constantly refering to button[count][insideCount], we’ll assign it later  

           

            --Position the button

            i.x = x;

            i.y = y;         

           

            --Give each a button a name

            i.myName = buttonImages[temp]

            i.number = totalButtons

            i.id=buttonid[#buttonImages]

            i.touch = game         

            i:addEventListener( “touch”, i )

            i.width=100

            i.height=100

            button[count][insideCount] = i      – put the button image in two-dimensional array

           

            --Remove button from buttonImages table

            table.remove(buttonImages, temp)

                       

            buttonCover[totalButtons] = display.newImage(“button.png”);

            buttonCover[totalButtons].x = x; buttonCover[totalButtons].y = y;

            buttonCover[totalButtons].width=100

            buttonCover[totalButtons].height=100

            totalButtons = totalButtons + 1

           

      end

end

[/lua]

Alternatively, to make it easier to remove the buttons later, put them in a one-dimensional array like this:

[lua]

local button = {}

for count = 1 , 4 do

      x = x + 100

      y = - 50

     

      for insideCount = 1 , 5 do

            y = y + 150

           

            --Assign each image a random location on grid

            temp = math.random( 1 ,#buttonImages)

     

            local i = display.newImage(buttonImages[temp] ".png");   – using ‘i’ is easier than

            – constantly refering to button[count][insideCount], we’ll assign it later  

           

            --Position the button

            i.x = x;

            i.y = y;         

           

            --Give each a button a name

            i.myName = buttonImages[temp]

            i.number = totalButtons

            i.id=buttonid[#buttonImages]

            i.touch = game         

            i:addEventListener( “touch”, i )

            i.width=100

            i.height=100

            button[#button+1] = i   – put the button image in one-dimensional array at next available slot

           

            --Remove button from buttonImages table

            table.remove(buttonImages, temp)

                       

            buttonCover[#button] = display.newImage(“button.png”);

            buttonCover[#button].x = x; buttonCover[totalButtons].y = y;

            buttonCover[#button].width=100

            buttonCover[#button].height=100

      end

end

[/lua]

thanks, puttin them in a one-dimensional array worked for me.