Alternately, you could create the grid as objects, and have them be “tappable”:
local row = 3 local column = 2 local cellWidth = 32 local cellHeight = cellWidth local cell = {} local centerX = display.contentCenterX-50 local centerY = display.contentCenterY-100 local function flipSize(obj) local function objReturn() obj:setFillColor(.75,.5,.25) transition.to(obj,{time=250, alpha=1, xScale=1}) end transition.to(obj,{time=500, xScale=.1, onComplete=objReturn}) end local function handleTile(self, event) if event.phase == "ended" then flipSize(self) print("self.idx = "..self.idx.." self.idy = "..self.idy) return true end end local function createGrid() for x = 1, row do -- column cell[x] = {} for y = 1, column do --row cell[x][y] = display.newRect(x\*cellWidth, y\*cellHeight,cellWidth,cellHeight) cell[x][y].strokeWidth = 1 cell[x][y]:setStrokeColor(1) cell[x][y]:setFillColor(0) cell[x][y].x = y\*(cellWidth)+centerX cell[x][y].y = x\*(cellHeight)+centerY cell[x][y].idx = x cell[x][y].idy = y cell[x][y].tapped = false cell[x][y].type = "ground" cell[x][y].isVisible = true cell[x][y].isHitTestable = true cell[x][y].touch = handleTile cell[x][y]:addEventListener("touch", cell[x][y]) end end end createGrid()
This is a useful option if you’re using sprites, and want to optimize performance by creating all objects at level generation, rather than create them upon user interaction.