As I said before I still think your way of learning is back to front - to use Jeff’s car analogy, it’s a lot easier to take a working car apart, figure out what everything does and put it back together again, than it is to design and build a new car from scratch.
But anyway, you need to think about each problem logically and break it down into small steps.
So - you have a grid of circles of different colours. If your objects are in a grid, it makes sense to have the data relating to them in a grid too.
[lua] 1 2 3 4 5
1 Red Green Blue Blue Yellow
2 Orange Purple Blue Red Pink
3 Brown Green Red Orange Yellow
4 Purple Green Blue Yellow Pink
5 Red Red Purple Orange Green[/lua]
At the moment, the circles are stored in a one-dimensional array called ‘allcoloredcircles’. The table above is two-dimensional, and it would make it easier to compare circles that are next to each other if the circles were stored like this.
You can do this using a loop after allcoloredcircles has been defined:
[lua]local allcoloredcircles = {}
for row = 1, 5, 1 do
allcoloredcircles[row] = {}
– initialises a table of columns for each row
end[/lua]
I have switched around the spawn function a little - it creates a local newCircle, applies positioning, assigns it a color value and physics body, and then inserts it into the correct position in the two-dimensional table.
[lua]local function spawncoloredcircle( )
– audio.play( popSound )
local color = math.random(1, 7) – store color choice for later
local randImage = coloredcircleGfx[color] – localised randImage
local newCircle = display.newImage( randImage )
local xPos = 3 + (col * 35) – adjust to get spacing right
local yPos = 105 + (row * 50) – adjust to get spacing right
newCircle.x = xPos
newCircle.y = yPos
newCircle.color = color – store color value
newCircle.delete = false – will need this later
physics.addBody( newCircle, { density=-1.0, friction=1.0, bounce=-2.0, radius = 27 } )
allcoloredcircles[row][col] = newCircle
– each circle can now be addressed by its row and column values
col = col + 1
if col == 6 then
col = 1
row = row + 1
end
coloredcircle:addEventListener( “touch”, dragBody )
end[/lua]
We can now find the color of any circle by getting the value
[lua]allcoloredcircles[3][1].color [/lua]
This will return the colour of the circle in the 3rd row, 1st column (in the table above that would be brown, although we are storing values rather than names so it would be the number between 1 and 7 that relates to brown).
At this stage I have to ask whether you are actually using the physics engine? What are these circles going to do? If all they are doing is staying in a grid, where you can swap them around by touching them and they will disappear when the same colours are next to each other, you don’t need physics at all…I suspect this is the case?
This function will check the grid for circles of the same colour:
[lua]local checkGrid = function ()
for row = 1, 5, 1 do – loop through every row
for col = 1, 5, 1 do – loop through every column in current row
local this = allcoloredcircles[row][col] – stores circle in current row & col
local across – stores the circle to the right temporarily
local down – stores the circle underneath temporarily
if this ~= nil then – only do check if circle exists
if col < 5 then – if col = 5 we don’t want to look at col 6 because it doesn’t exist
across = allcoloredcircles[row][col+1]
if this.color == across.color then
this.delete = true – sets flag to remove object later
across.delete = true – sets flag to remove object later
end
end
if row < 5 then – if row = 5 we don’t want to look at row 6 because it doesn’t exist
down = allcoloredcircles[row+1][col]
if this.color == down.color then
this.delete = true – sets flag to remove object later
down.delete = true – sets flag to remove object later
end
end
end
end
end
end[/lua]
Now you can check the state of the grid at any time by calling checkGrid(). You could put this in your enterFrame function to check every frame, or perhaps in your swap circles function to check only after some circles have been swapped.
Then you need a function which will remove all circles flagged for deletion:
[lua]local removeAdjacent = function ()
for row = 1, 5, 1 do – loop through every row
for col = 1, 5, 1 do – loop through every column in current row
local this = allcoloredcircles[row][col] – stores circle in current row & col
if this ~= nil then
if this.delete == true then
this:removeSelf() – remove flagged circles
end
end
end
end
end[/lua]
You could call this function using timer.performWithDelay(1000, removeAdjacent) to allow any swapped circles to move into their new position before removing them.
[import]uid: 93133 topic_id: 22812 reply_id: 91243[/import]