Grid - Change Alpha On Multiple Tiles

When a tile is tapped, I have a event to capture this.  My question is, how would I be able to access the tiles around the tile that is tapped.

Example:

Lets say tile in position [2][2] is tapped and the chipTapped function is called. This would change the alpha in tile[2][2].  How would I also change the alpha for the tile in position [2][3]?

Thanks for any help!

Warren

local function chipTapped(event)

    if event.target.alpha == 1 then

        event.target.alpha = .2

    else

        event.target.alpha = 1

    end

    

end


for row = 1, numRows do

for col = 1, numCols do

                i=i+1

                if puzzle[1][i] == “” then

                  local chip = display.newImageRect(“images/blank.png”,20,20) 

                else

                  chip = display.newImageRect(“images/black.png”,20,20)   

                end

–chip.width = chipWidth – for testing only

–chip.height = chipHeight – for testing only

chip.x = xPos + col * (chipWidth + colSpace) - chipWidth/2 - colSpace

chip.y = yPos + row * (chipHeight + rowSpace) - chipHeight/2 - rowSpace

chip.gridPos = {x=col, y=row}

                chip.letter = puzzle[1][i]

                chip.inx = i

chip:addEventListener(“tap”, chipTapped)

end

end

I had to hack it a bit to get it to work but hopefully you get the idea.  You need a chip table that you can reference in your touch listener kinda like this:

local chip = {} local chipWidth = 20 local chipHeight = 20 local colSpace = 10 local rowSpace = 10 local function chipTapped(event) local t = event.target if t.alpha == 1 then t.alpha = .2 else t.alpha = 1 end --We can use this chips "gridPos" properties to reference other chips local col = t.gridPos.x local row = t.gridPos.y --To change 1 row above chip[row-1][col]:setFillColor( 1,0,0 ) --To change 2 rows above chip[row-2][col]:setFillColor( 0,1,0 ) --To change 1 column left chip[row][col-1]:setFillColor( 0,0,1 ) end ---------------------------------------------------------------------------------------------------- for row = 1, numRows do chip[row] = {} for col = 1, numCols do --Create chip local c = display.newRect(0, 0, chipWidth, chipHeight) c.x = col \* (chipWidth + colSpace) - chipWidth/2 - colSpace c.y = row \* (chipHeight + rowSpace) - chipHeight/2 - rowSpace c.gridPos = {x=col, y=row} --c.letter = puzzle[1][i] --c.inx = i c:addEventListener("tap", chipTapped) --Insert into chip table chip[row][col] = c end end

I had to hack it a bit to get it to work but hopefully you get the idea.  You need a chip table that you can reference in your touch listener kinda like this:

local chip = {} local chipWidth = 20 local chipHeight = 20 local colSpace = 10 local rowSpace = 10 local function chipTapped(event) local t = event.target if t.alpha == 1 then t.alpha = .2 else t.alpha = 1 end --We can use this chips "gridPos" properties to reference other chips local col = t.gridPos.x local row = t.gridPos.y --To change 1 row above chip[row-1][col]:setFillColor( 1,0,0 ) --To change 2 rows above chip[row-2][col]:setFillColor( 0,1,0 ) --To change 1 column left chip[row][col-1]:setFillColor( 0,0,1 ) end ---------------------------------------------------------------------------------------------------- for row = 1, numRows do chip[row] = {} for col = 1, numCols do --Create chip local c = display.newRect(0, 0, chipWidth, chipHeight) c.x = col \* (chipWidth + colSpace) - chipWidth/2 - colSpace c.y = row \* (chipHeight + rowSpace) - chipHeight/2 - rowSpace c.gridPos = {x=col, y=row} --c.letter = puzzle[1][i] --c.inx = i c:addEventListener("tap", chipTapped) --Insert into chip table chip[row][col] = c end end