couple more questions before tweaking code.

I’v gotten some incredible help from people so I’m hoping the trend continues. I’ve learned a lot (still lot’s to learn) but with the help of others I am slowly understanding the corona/lua language.

Anyway, here goes.
Problem #1.
When I try to spawn objects (25 of them) they never spawn 25. They are always spawned from about 20 to 25 but never exactly 25. I’ve tried these 2 different lines of code with no difference:

(1.) for a = 1, 25, 1 do
spawncoloredcircle()
end
(2.) timer.performWithDelay(interval, spawncoloredcircle, 25)

problem #2.
I cannot figure out how to tell wehn 2 or more objects of the same color are next to each other how to remove all of them at the same time. What i mean is that I spawn 25 different circles that all look the same and have the same physics but are different colors. How can I get corona to recognize that if there are circles with the same colors next to each other that they get removed from the screen.

I’m assuming I need to index the spawned images some way so they all have a unique identifier but not sure if that’s correct or not.

Thanks for any help. [import]uid: 127842 topic_id: 22812 reply_id: 322812[/import]

Hello again :stuck_out_tongue:

Could you post up the whole code so I can see what you have got so far?

Also please use code tags

[code]

[code]

insert code here

[/code] [import]uid: 84637 topic_id: 22812 reply_id: 91116[/import]

#1

for a = 1, 25 do  
 print ("a: "..a)  
end  

counts from 1 to 25. the error has to be in your spawncoloredcircle() function.
#2
when working with table objects you can add new “properties”

local myObject = {}  
myObject.object = display.newRect( 10, 10, 100, 20 )  
myObject.ofType = "explosive"  
print ("My Object is type: " .. myObject.ofType)  

hope that helps (a bit)

-finefin

[import]uid: 70635 topic_id: 22812 reply_id: 91117[/import]

If you don’t get 25, then there is something wrong in the spawncoloredcircle() function.

What that should be doing is creating new objects, presumably with physics, which will drop or bounce or something into known possible locations.

Assuming the colours are from a finite set of (say) 16, then the colour of each can be represented as a simple integer.
And that can be assigned to a property of the object.

Ignoring the physics for the moment,

The spawn code logic will go like this:
local Circles{}
local uniqueID =0

local spawncoloredcircle()
local newCircle = display.NewOval(…stuff)
local colIndex = math.random(1,16)
newCircle:setFillColor ( …whatever colour that random number suggests)
newCircle.colindex = colIndex --this records the colour of the circle in an easy form
newCircle.sameas = uniqueID
newCircle.uniqueID = uniqueID

–place it randomly
newCircle.x = math.random(newCircle.width,display.contentWidth-newCircle.width)
newCircle.y = newCircle.height * -2
display:insert(newCircle)
end

Now, each one appears , it will start to drop.
Eventually, I expect it will end up in what amounts to a grid.

You need a loop that (at the simplest) checks the neighbours of each visible circle.
For each circle in the collection of circles, check for other circles which are newCircle.width or newCircle.height away from yourCircle.x and yourCircle.y
and which have the same .colIndex

Each one you find as you go, set .sameas = your .uniqueID

If the total amounts to 3 or more, go through the circles again, deleting your original and any marked with .sameas = your uniqueID

[import]uid: 108660 topic_id: 22812 reply_id: 91120[/import]

Simplify:

here is some basic code to prove you get 25 items.
Note that I pass the id into the create function.

[code]
local function spawncoloredcircle( a)

–rect was easier to type
local coloredcircle = display.newRect(0,0,20,20)

–a % 5 means divide by 5, tell me whats left … gives values of 0…4
coloredcircle.x = (a % 5 ) * 40 + 100
–now we do the divide by 5, take the integer, use it as the row number
coloredcircle.y = math.floor (a / 5 ) * 40 + 60
–use the passed id
coloredcircle.id = a

–commented stuff
–physics.addBody( coloredcircle, { density=-1.0, friction=1.0, bounce=-2.0, radius = 10 } )
–coloredcircle:addEventListener( “touch”, dragBody )
end
for a = 0, 24 do
spawncoloredcircle(a)
end
[/code] [import]uid: 108660 topic_id: 22812 reply_id: 91128[/import]

@donuan

Please use the code tags like i instructed above. Everyone uses them and it makes reading your code easy. Without them it makes life hard, thanks [import]uid: 84637 topic_id: 22812 reply_id: 91132[/import]

And if you change your code to look like mine?
My code draws exactly 25 things on the screen, in 5 rows of 5
Cant fail

So if yours does not, you are drawing off screen or on top of each other.
But use the index to determine the starting position like my code?

here is it again without the comments.
Note the way that the position is based on the index number

[code]
local function spawncoloredcircle( a)
local coloredcircle = display.newRect(0,0,20,20)
coloredcircle.x = (a % 5 ) * 40 + 100
coloredcircle.y = math.floor (a / 5 ) * 40 + 60
coloredcircle.id = a

end

for a = 0, 24 do
spawncoloredcircle(a)
end

[code]
[import]uid: 108660 topic_id: 22812 reply_id: 91135[/import]

what you do is to put
[cpp]

  

[/cpp]

around your code sections.
Then they get coloured like proper code, and given line numbers. [import]uid: 108660 topic_id: 22812 reply_id: 91137[/import]

uuhm… use the code tags here in the forum, not in your code! [import]uid: 70635 topic_id: 22812 reply_id: 91140[/import]

ha!

On the upside, I figured out the 25 spawn problem…sort of…
it had to do with this:
randImage = coloredcircleGfx[math.random( 1, 7 )]
I was referencing too many images (1, 8 ) when I only had 7 images.
So simple and yet so dificult.

Thanks everyone.
Now to figure out the collision between like objects [import]uid: 127842 topic_id: 22812 reply_id: 91141[/import]

You could try. Zip it up and tell me where to look.

Bear in mind that your code has a lot of dependencies:
I couldn’t take what you posted here and run it at all, which is why I gave you the super stripped down version.
[import]uid: 108660 topic_id: 22812 reply_id: 91161[/import]

The code you posted is only part of the whole.
Its a bit like asking someone to drive your car by giving them the exhaust manifold to look at.

So if I or anyone took *only* the code you posted and pasted it into a .lua file and hit run, it just collapses in a heap.

The line require “physics” isn’t there, for example.
#allcoloredcircles is not defined.
coloredcircleGfx[] is not populated, and the graphics on which it depends aren’t present.
I’ll be free at the weekend if you can wait that long. send something to my name @ u r s a s o f t w a r e . c o m

[import]uid: 108660 topic_id: 22812 reply_id: 91221[/import]

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]

Yeah, good spot with the event listener, my mistake.

You need to have

[lua]checkGrid()[/lua]

somewhere in your code to fire the function. I suggest firing it at the end of your swap circles code, and it won’t work at all if you haven’t already spawned the 25 circles. Also make sure the function is above the swap circle function within the code.

This function won’t actually produce any visible output, so you could put some print statements in the sections that set the delete flags:

[lua]-- ‘across’ section
print(“Circle in row:”…row…", column:"…col…" flagged for deletion")

print(“Circle in row:”…row…", column:"…col+1…" flagged for deletion")

– ‘down’ section
print(“Circle in row:”…row…", column:"…col…" flagged for deletion")

print(“Circle in row:”…row+1…", column:"…col…" flagged for deletion")[/lua]

The circles themselves won’t actually be removed from the screen until you call

[lua]removeAdjacent()[/lua]

Yes, allcoloredcircles[3][1].color was just an example for how you would find the color for a given circle in the grid.
[import]uid: 93133 topic_id: 22812 reply_id: 91282[/import]

What errors are you getting? One thing that occurs is I used ‘row’ and ‘col’ as the accumulators for the loops, when they’re already being used as variables which isn’t too clever.

Try changing all the references to ‘row’ and ‘col’ to ‘r’ and ‘c’ within the checkGrid and removeAdjacent functions, [import]uid: 93133 topic_id: 22812 reply_id: 91483[/import]

Tried removing all row/col to r and c
placed checkGrid () after the 25 circles have spawned and getting this error:

Runtime error attempt to index field ‘?’ ( a nil value)

I’m thinking I maybe don’t have my functions in the right order? ( although I’ve tried different variations)
[import]uid: 127842 topic_id: 22812 reply_id: 91491[/import]

It should also tell you what line number the error is happening in so we can pinpoint the exact problem.

Also change this function to:

[lua]local allcoloredcircles = {}

for r = 1, 5, 1 do

allcoloredcircles[r] = {}
– initialises a table of columns for each row
end [/lua] [import]uid: 93133 topic_id: 22812 reply_id: 91495[/import]

donuan you need to use code tags: Your posts are hard to read without them.
http://developer.anscamobile.com/filter/tips [import]uid: 7177 topic_id: 22812 reply_id: 91523[/import]

local allcoloredcircles = {}  
   
for r = 1, 5, 1 do-- initialises a table of columns for each row  
   
 allcoloredcircles[r] = {}   
   
end  

…needs to have 5 values actually put into the {} part, otherwise all you have at the end of that function is an array of 5 empty arrays.
People aren’t writing your app for you (you actually said you didn’t want that anyway)

Try adding print statements to your code so that you can see the value of things before they get used.
Debugging is terrible in Corona, but common sense is useful.
Don’t throw your hands up in the face of every single error: break the job down into bits and check each bit is doing what it should, one by one.

[import]uid: 108660 topic_id: 22812 reply_id: 91645[/import]

@jeff - the array is populated later on in the spawn function, that part just sets the row arrays up.

@donuan - I think you may be calling checkGrid before the 25 circles are spawned. They are set up to spawn every 10ms, so thats 250ms before they are all on screen. So you need to put a delay on the checkGrid() call.

Also in the code you posted you have allcoloredcircles[row][c] instead of [r][c].

And you are defining ‘local allcoloredcircles = {}’ three times. [import]uid: 93133 topic_id: 22812 reply_id: 91674[/import]