How can I set properties to object in table?

Hello everyone. I am making game like sea war. So I have matrix 10 x 10 and i wirte such code:

 for i = 1, 10 do

      cellTab[i] = {}   

      xoff = 50

      yoff = yoff + 50

      for j = 1, 10 do

          local cell = display.newImageRect( mainGroup, objectSheet, 1, 50, 50 )

          cell.x = xoff 

          cell.y = yoff 

          cellTab[i][j] = cell

          xoff = xoff + 50 

      end

end 

I will make some action on every cell in matrix (for example change transperancy). So how i can do this to specific cell (for example 9 row 1 column)?

Or i am wrong in my start way, and need other stucture? Thanks for help. 

P.s. sorry for my english. 

I have solved this task already, but need to ask how can i remove all event listeners that I assigned in this function:

local function emptyMesh ()

        local xoff = 0

        local yoff = 0

        for i = 1, 10 do

             cellEmpTab[i] = {}     – create a new row

             xoff = 50 

             yoff = yoff + 50

                      for j = 1, 10 do

                                local cellEmp = display.newImageRect( mainGroup, objectSheet, 6, 50, 50 )

                                cellEmp:addEventListener( “tap”, placeSph )

                                cellEmp.x = xoff 

                                cellEmp.y = yoff 

                                cellEmpTab[i][j] = cell

                                xoff = xoff + 50  

                       end

        end 

end

When i load picture from image sheet, and put object in table, i assign event listener to every picture of empty cell. So i when user tap on some cell i can put another image object in this cell. But the number of cell for tap is resticted by six. After six tap on some six cells I no longer need these event listeners (100, 1 for every cell in matrix 10 x 10). How can i remove them all?

The way that I would go about doing something like this would be to just create a single invisible rect on top of them and only add a touch event listener to it. Then I’d use the touch event location to determine which imageRect was being pressed.

However, with your case, in order to remove an event listener from a display object, you need to either destroy said display object or you need to have a reference to it that you can remove the event listener from. Now, you may have a typo here: “cellEmpTab[i][j] = cell”. If that is a typo and you change that to “cellEmpTab[i][j] = cellEmp”, then you will have a reference to these display objects that you can use to later remove the event listeners from.

With that change/fix, you could remove them all via:

for i = 1, #cellEmpTab do for j = 1, #cellEmpTab[i] do cellEmpTab[i][j]:removeEventListener( "tap", placeSph ) end end

Thanks a lot.

In this case you mean use event.x  and event.y  from this invisible rect? As i understand some mathematical calculations are needed to know on which cell tap was?

And yes this is mistake in my code, sorry.

Precisely.

If you know (and you do know) where the first and last rows and columns start, you can carry out a simple calculation to find out where the touch is occurring.

Here’s some pseudo code of how it can be done:

-- Calculate the minimum and maximum bounds of your grid, then: function getCell( event ) if event.phase == "began" then -- I always use touch events instead of taps. if event.x \>= bounds.xMin and event.y \<= bounds.xMax then if event.y \>= bounds.yMin and event.y \<= bounds.yMax then local row = math.ceil( (event.y - bounds.yMin)/cellSize ) local column = math.ceil( (event.x - bounds.xMin)/cellSize ) -- And now you know that cell[row][column] was touched. end end end end

Thanks.

May I ask some more questions about sea war game.

How can I rotate user turn (user - ai - user - ai …). When user tap on second matrix (ai field), i need temporarily block all tap event listeners on this field. Is there some block command or trick for that?

You can just add a conditional check: " if canPress then" at the start of the function. Whenever it is the AI’s turn, just set canPress to false and the player’s touches will be ignored.

I have solved this task already, but need to ask how can i remove all event listeners that I assigned in this function:

local function emptyMesh ()

        local xoff = 0

        local yoff = 0

        for i = 1, 10 do

             cellEmpTab[i] = {}     – create a new row

             xoff = 50 

             yoff = yoff + 50

                      for j = 1, 10 do

                                local cellEmp = display.newImageRect( mainGroup, objectSheet, 6, 50, 50 )

                                cellEmp:addEventListener( “tap”, placeSph )

                                cellEmp.x = xoff 

                                cellEmp.y = yoff 

                                cellEmpTab[i][j] = cell

                                xoff = xoff + 50  

                       end

        end 

end

When i load picture from image sheet, and put object in table, i assign event listener to every picture of empty cell. So i when user tap on some cell i can put another image object in this cell. But the number of cell for tap is resticted by six. After six tap on some six cells I no longer need these event listeners (100, 1 for every cell in matrix 10 x 10). How can i remove them all?

The way that I would go about doing something like this would be to just create a single invisible rect on top of them and only add a touch event listener to it. Then I’d use the touch event location to determine which imageRect was being pressed.

However, with your case, in order to remove an event listener from a display object, you need to either destroy said display object or you need to have a reference to it that you can remove the event listener from. Now, you may have a typo here: “cellEmpTab[i][j] = cell”. If that is a typo and you change that to “cellEmpTab[i][j] = cellEmp”, then you will have a reference to these display objects that you can use to later remove the event listeners from.

With that change/fix, you could remove them all via:

for i = 1, #cellEmpTab do for j = 1, #cellEmpTab[i] do cellEmpTab[i][j]:removeEventListener( "tap", placeSph ) end end

Thanks a lot.

In this case you mean use event.x  and event.y  from this invisible rect? As i understand some mathematical calculations are needed to know on which cell tap was?

And yes this is mistake in my code, sorry.

Precisely.

If you know (and you do know) where the first and last rows and columns start, you can carry out a simple calculation to find out where the touch is occurring.

Here’s some pseudo code of how it can be done:

-- Calculate the minimum and maximum bounds of your grid, then: function getCell( event ) if event.phase == "began" then -- I always use touch events instead of taps. if event.x \>= bounds.xMin and event.y \<= bounds.xMax then if event.y \>= bounds.yMin and event.y \<= bounds.yMax then local row = math.ceil( (event.y - bounds.yMin)/cellSize ) local column = math.ceil( (event.x - bounds.xMin)/cellSize ) -- And now you know that cell[row][column] was touched. end end end end

Thanks.

May I ask some more questions about sea war game.

How can I rotate user turn (user - ai - user - ai …). When user tap on second matrix (ai field), i need temporarily block all tap event listeners on this field. Is there some block command or trick for that?

You can just add a conditional check: " if canPress then" at the start of the function. Whenever it is the AI’s turn, just set canPress to false and the player’s touches will be ignored.