How can I prevent random spawning of multiple object to the same location?

Thanks, I thought you meant something else. I think this is were my problem lies. I didn’t realize I could have the ture/false right off of myGridObjs[idx].

I though I needed to do myGridObjs[idx].isOccupied == false

More so though I think there’s an issue with my program when it fines the grid idx to be true. I haven’t had a chance to work on it yet today but I will definitely put everyone’s notes to work soon.

Looks like you’re still learning the basics of Lua, so maybe code is more useful than words. Here’s a sample using approach “A” I described above. Just drop it in some folder as main.lua and run it.

math.randomseed(os.time()) local GRID\_COLS = 4 local GRID\_ROWS = 6 local GRID\_SPACING = 40 local grid = {} -- set up the grid for i = 1, GRID\_ROWS do grid[i] = {} for j = 1, GRID\_COLS do local tile = display.newCircle(j \* GRID\_SPACING, i \* GRID\_SPACING, 4) tile.isOccupied = false grid[i][j] = tile end end -- make a list of unoccupied grid tiles local findFreeTiles = function() local freeTiles = {} for i = 1, #grid do for j = 1, #grid[i] do if not grid[i][j].isOccupied then table.insert(freeTiles, grid[i][j]) end end end return freeTiles end -- spawn a thing on top of a grid tile local spawnThing = function(tile) local thing = display.newCircle(tile.x, tile.y, 20) thing:setFillColor(1, 0, 0) thing:toFront() tile.isOccupied = true end -- spawn a thing and place it on a random free grid tile local placeRandomThing = function() local freeTiles = findFreeTiles() if #freeTiles == 0 then print("No more room available, can't spawn more things!") return end local randomTile = freeTiles[math.random(1, #freeTiles)] spawnThing(randomTile) end -- make a button that spawns things local button = display.newText("Spawn Thing", display.contentCenterX, 10, native.systemFont, 16) button:addEventListener("tap", placeRandomThing)

For what it’s worth, this weeks tutorial is about working with grids in games:

https://coronalabs.com/blog/2015/04/07/tutorial-working-with-a-grid-layout/

Rob

Thanks _memo, thanks Rob

I been stuck on duty and haven’t had a chance to work on my game but can’t wait to look through the code and tutorial. Yes I am new to this kind of programming and lua. Coming from a SCADA, PLC background the logic and concepts make sense but learning to understand the language is where I am at. I actually started using a drag and drop program first as it’s was very close to my SCADA work but learned about Corona shortly after, which is more or the route I was looking for. Thanks again guys as I have been learning a lot here and through the various tutorials you have available.

Mahalo,

Kaleo

Finally got to work on the project. I checked out the code and can see where I went wrong. Also started blending the other aspects of my game with the code you provided and edited a little of your code as needed. Thanks again.

Still working out some kinks but I’m on my way.

Mahalo,

Kaleo