Best method for destroying buildings

Hello all, Brad here. I’ve currently developing an app that at the beginning spawns buildings 6x10. So bascially they are 6 squares across and 10 rows of squares (buildings) I’m trying to make it to where when the game starts the all the sqaures are there, and when the user clicks 1 square it destroys the square. What would be the best method to doing this? I know a little about grids, but I also tried creating each square 1 by 1. I didint know if there was an easier way than to make 60 squares 1 by 1 and then 60 touch even listeners. Thanks! 

Are you talking about a building destruction mechanic like space invaders?

1480671860spaceinvaders.jpg

As far as creating 60 objects w/ 60 touch listeners?  That is easy, but you really only need one common listener:

local function onTouch( self, event ) display.remove( self ) return true end for i = 1, 10 do for j = 1, 6 do local tmp = display.newRect( i \* 15, j \* 15, 10, 10 ) tmp.touch = onTouch tmp:addEventListener("touch") end end

^^ thats giving me an error, I cant find it. says : ‘)’ expected near ‘,’

not like space invaders. Say the iphone is vertical .I need 6 squares across by 10 rows down. When someone click a single square i need it to be destroyed. 

  1. Code corrected.  Try it again. 

Fixed this line: 

local function onTouch( self, event )

If there are still syntax errors, track them down one by one and correct them. I haven’t typed this in and run it.

  1. You want to put squares on the screen and pop them when people tap them?

Again, try the code above?

Personally I would have a runtime:ontouch() event and use that to calculate possible touches with active objects.  If you are tracking the x,y coords of moving objects this should be relatively simple. IMHO, much cleaner than multiple event handlers.

(Good point rg)

Hi.  I don’t think @vw17 is talking about collisions.  The question was about touches specifically.

I’m just replying, because I don’t want to start a new person (or anyone else who reads this later) thinking the term collision and touch are the same, because they are two different concepts.

Sweet it works! so basically I need units moving from the bottom to the top. But when it starts i need the user to be able to click the each building to make a path to the top for the units to go up through. 

Anyone?

Are you talking about a building destruction mechanic like space invaders?

1480671860spaceinvaders.jpg

As far as creating 60 objects w/ 60 touch listeners?  That is easy, but you really only need one common listener:

local function onTouch( self, event ) display.remove( self ) return true end for i = 1, 10 do for j = 1, 6 do local tmp = display.newRect( i \* 15, j \* 15, 10, 10 ) tmp.touch = onTouch tmp:addEventListener("touch") end end

^^ thats giving me an error, I cant find it. says : ‘)’ expected near ‘,’

not like space invaders. Say the iphone is vertical .I need 6 squares across by 10 rows down. When someone click a single square i need it to be destroyed. 

  1. Code corrected.  Try it again. 

Fixed this line: 

local function onTouch( self, event )

If there are still syntax errors, track them down one by one and correct them. I haven’t typed this in and run it.

  1. You want to put squares on the screen and pop them when people tap them?

Again, try the code above?

Personally I would have a runtime:ontouch() event and use that to calculate possible touches with active objects.  If you are tracking the x,y coords of moving objects this should be relatively simple. IMHO, much cleaner than multiple event handlers.

(Good point rg)

Hi.  I don’t think @vw17 is talking about collisions.  The question was about touches specifically.

I’m just replying, because I don’t want to start a new person (or anyone else who reads this later) thinking the term collision and touch are the same, because they are two different concepts.

Sweet it works! so basically I need units moving from the bottom to the top. But when it starts i need the user to be able to click the each building to make a path to the top for the units to go up through. 

Anyone?