Help with some Whac- a -mole game logic

Hello , I have a problem with spawning objects in desired positions like i have in the game a grid of 4 x 3 holes

O O O
O O O
O O O
O O O

Now the idea would be that everytime i call an object to appear , it will have to randomly select one of those 12 space and appear IF the space is empty due to the game being able to have more than one object on the screen at once.
I have found solutions for this problem but it involves the use of a WHILE but it really breaks performance in the game so i wanted to know if there is different solution for this problem. [import]uid: 127101 topic_id: 28096 reply_id: 328096[/import]

There are dozens of ways to do this. I think the way I would attack it is to actually create all 12 moles and use the .isVisible flag to hide them. Then you could use a timer to randomly turn one on. When it’s time to hide comes up, you hide it by making it invisible again (actually setting the alpha to 0 might be better than .isVisible since you can use transitions to fade and show the mole.)

Each mole would have a touch event handler and if it was hit, run your scoring logic, and then hide the mole when done. [import]uid: 19626 topic_id: 28096 reply_id: 113513[/import]

the problem with that is that i have diffrerent kinds of moles and each kind of mole has a different event handler , like some would give you points if you touch em , others will take away points , etc …

I mean i have though of a way where I manually create a table with all the possible combinations of moles and they would be appearing in a wave like mode. like each wave it would randomly select any of the combinations available for the level but yeah … there are 12 holes and right now 6 kinds of moles so yeah, thats a lot of combinations. [import]uid: 127101 topic_id: 28096 reply_id: 113518[/import]

There’s another way to do this:

  1. Create a temporary array
  2. Perform a sweep of all the empty holes and add the position into the array
  3. Pick a random position from the array, which the value will correspond to the empty hole.

Your code could essentially look like this:[code]

local whichhole
local emptyhole = {}

– find an empty hole
for i = 1, 12 do
if not mole_present[i] then
– add that empty hole number to the list
emptyhole[#emptyhole + 1] = i
end
end

if #emptyhole > 0 then
– pick one of the empty ones from the list
whichhole = emptyhole[math.random( #emptyhole)]
else
– all holes are in use!
end
[/code]

Hope that helps!
[import]uid: 6084 topic_id: 28096 reply_id: 113519[/import]

Oh yeah! that would work wonderful and its wont be that hard to implement with all the different kids of moles . [import]uid: 127101 topic_id: 28096 reply_id: 113521[/import]

Another way, which may be better suited is to have an array that represents each 12 position. Each array entry is another table of information, including the X, Y locations of the spots and other information. It would also hold each items display object that you could create based on which mole is showing up, set additional properties for that mole etc. Then on tap, you would just remove the mole using removeSelf() and set the entry in the table to nil for garbage collection. But the table would still be holding the X, Y locations.

Then you could do a simple for loop to run through the 12 spots and see if all of the display entries are nil, if so create your new mole.

local hole = {}  
hole[1].x = 100  
hole[1].y = 100  
hole[1].mole = nil  
hole[2].x = 100  
hole[2].y = 100  
hole[2].mole = nil  
-- etc.  
  
local function whackMyMole(event)  
 if event.phase == "began" then  
 slot = event.target.index  
 hole[slot].mole:removeSelf()  
 hole[slot].mole = nil  
 end  
end  
  
local function spawnMole()  
 local haveMole = false  
  
 for i = 1, 12, do  
 if hole[i].mole then haveMole = true end  
 end  
 if not haveMole then  
 local whichHole = math.random(12)  
 hole[whichHole].mole = display.newImageRect("mymole.png", 64, 64)  
 -- plus whatever you need to do, add to a group, etc.  
 hole[whichHole].mole:addEventListener("touch",whackMyMole)  
 hole[whichHole].mole.index = whichHole  
 end  
end  

Or something like that… [import]uid: 19626 topic_id: 28096 reply_id: 113533[/import]