I ultimately chose StarCrunch’s suggestion to implement as it seemed to fit best with what I had already written. With that said, I do have some more questions. As always, any help is appreciated.
The game starts with the following function:
local function gameStart() blueBallSpawn() spawnTable[1].x = grid[3][4].x spawnTable[1].y = grid[3][4].y redBallSpawn() spawnTable[2].x = grid[3][3].x spawnTable[2].y = grid[3][3].y end gameStart()
The functions to spawn different colored balls are all virtually the same, here’s blueBallSpawn() as an example.
function blueBallSpawn() local spawns = spawn( { image = "blueball.png", objTable = spawnTable, hasBody = true, group = localGroup, myName = "blueBall", Id = "blueBall", x = xNew, y = yNew, width = 128, height = 128, anchorX = 0.5, anchorY = 0.5, } ) tracker = tracker + 1 end
Here’s the tap function which handles the tap events for balls in the middle of the grid. I’ve included only the down-arrow button for simplicity.
local function ballTapped(event) if myButtons[1] == nil then for col = 2, 4 do for row = 2, 4 do local t = event.target xNew= t.x yNew = t.y if t.x == grid[col][row].x and t.y == grid[col][row].y then if t.Id == "blueBall" then blueBallSpawn() blueBallSpawn() elseif t.Id == "purpleBall" then blueBallSpawn() redBallSpawn() elseif t.Id == "redBall" then redBallSpawn() redBallSpawn() elseif t.Id == "yellowBall" then yellowBallSpawn() yellowBallSpawn() elseif t.Id == "orangeBall" then redBallSpawn() yellowBallSpawn() elseif t.Id == "greenBall" then yellowBallSpawn() blueBallSpawn() end t:removeSelf() t = nil local function removeButtons() myButtons[1]:removeSelf() myButtons[1] = nil end -- 'onRelease' event listener for buttons local function onBtnRelease(event) local target = event.target if target.myId == "Down" then audio.play(splitSoundFX) transition.to(spawnTable[tracker - 1],{time = 50, x = grid[col][row].x, y = grid[col][row].y - 128, onComplete = combineObj} ) transition.to(spawnTable[tracker], {time = 50, x = grid[col][row].x, y = grid[col][row].y + 128, onComplete = combineObj}) removeButtons() end return true -- indicates successful touch end myButtons[1] = widget.newButton{ labelColor = { default={0}, over={128} }, defaultFile="arrow.png", overFile="arrow.png", width=128, height=128, onRelease = onBtnRelease } myButtons[1].x = grid[col][row].x myButtons[1].y = grid[col][row].y + 128 myButtons[1].myId = "Down" end end end end end
And finally here is the code, based on StarCrunch’s suggestion, to handle the event of two balls occupying the same tile.
local function Index (column, row) return (row - 1) \* numCols + column end local ObjectInCell = {} local function combineObjects(object, other) if object.x == other.x and object.y == other.y then if object.Id == "redBall" and other.Id == "blueBall" then xNew = object.x yNew = other.y display.remove(object) object = nil display.remove(other) other = nil purpleBallSpawn() elseif object.Id == "blueBall" and other.Id == "redBall" then xNew = object.x yNew = object.y display.remove(object) object = nil display.remove(other) other = nil purpleBallSpawn() end end end local function onEnterCell (object, column, row) local index = Index(column, row) local other = ObjectInCell[index] if other then local new = combineObjects(object, other) ObjectInCell[index] = new -- put the combo object in `other`'s place else ObjectInCell[index] = object -- cell open: occupy it end end local function combineObj() for col = 1, 5, 1 do for row = 1, 5, 1 do for i = 1, #spawnTable, 1 do onEnterCell(spawnTable[i], col, row) end end end end
To start there is a red ball at grid[3][3] and a blue ball at grid[3][4]. Tapping the red ball will produce a down arrow. If the down arrow is tapped, two new red balls will spawn on top of the original. Using transitions, they will move up one tile and down one tile. The original ball is removed. When the red ball moves to the tile with the blue ball, they are combined and a purple ball spawns. Here are the problems. First, another purple ball also spawns at grid[3][3], the original position of the red ball. As well, as soon as the combined purple spawns at grid[3][4], an arrow overlay pops, even though I haven’t tapped on it. If I split the newly combined purple ball, it splits into two purple balls. Splitting those purple balls results in the expected split of red ball and blue ball. I’ve made progress, but feel like I’ve hit a snag. Any help would be appreciated. Thanks, Jim