Reputation: 145
I seem to be having an issue which I cannot solve properly and need some help. Looking at the image below, I am spawning shapes from a table called enemies and am trying to check if their x positions are occupied by a spawn, if so, then slightly move it left or right so it pops out enough to hit them as they move downwards. The image shows a problem where shapes are stacked behind each other.
Spawn function (called from the game loop)
local xPos = math.random(22,279)
local r = math.random(1, #spawnData)
local sd = spawnData[r] -- get the spawn data for this enemy
local s = display.newSprite(sd.imgSheet, sd.seqData)
s.name = sd.name
physics.addBody(s, {isSensor = true})
s:setSequence(sd.seq)
s:setFrame(sd.frame)
s.y = display.contentHeight - 550 -- Create the enemies using spawnData
local ok = false -- A flag to say whether we found a valid X position
local gap = 2 -- Set the minimum gap in pixels between enemies
local err = 0 -- An error flag to avoid the game slowing down if too many enemies
while ok == false do -- Tell it to loop until a valid X position is found
xPos = math.random(20,300) -- give a new X position
ok = true -- Until it has checked against other enemies, assume it is ok for now
for i = 1, #enemies, 1 do -- Loop through the previously spawned enemies
local e = enemies[i] -- Link to this enemy
local dist = mAbs(e.x - xPos) -- Work out the distance between this enemy and the random position chosen
if dist < (e.width + gap) then ok = false end -- If the enemies are too close, try agian
end
err = err + 1 -- Increase the count of attempts to find a valid position
if err > 50 then ok = true end -- After 50 tries, assume there is no empty space and just spawn wherever
end
s.x = xPos
enemies[#enemies+1] = s
enemyGroup:insert(s)
Upvotes: 1
Views: 80
Reputation: 322
You have two snippets of code that aid this error.
In this first snippet, you are looking at every sprite that has existed and trying to find an appropriate x position. With many sprites, the error becomes more prone. Try to only look at the previous Sprite's x position.
for i = 1, #enemies, 1 do -- Loop through the previously spawned enemies
local e = enemies[i] -- Link to this enemy
local dist = mAbs(e.x - xPos) -- Work out the distance between this enemy and the random position chosen
if dist < (e.width + gap) then ok = false end -- If the enemies are too close, try agian
end
In this next snippet, after 50 errors are made, it places the sprite anywhere. You are allowing this spawn to occur. Try a larger value. However, that is going to only postpone the issue.
err = err + 1 -- Increase the count of attempts to find a valid position
if err > 50 then ok = true end -- After 50 tries, assume there is no empty space and just spawn wherever
Edit:
Here is what I would do. There may be errors because I did not test it. Hopefully it does give you the idea of what should be done, though.
local xPos = math.random(22,279)
local r = math.random(1, #spawnData)
local sd = spawnData[r] -- get the spawn data for this enemy
local s = display.newSprite(sd.imgSheet, sd.seqData)
s.name = sd.name
physics.addBody(s, {isSensor = true})
s:setSequence(sd.seq)
s:setFrame(sd.frame)
s.y = display.contentHeight - 550 -- Create the enemies using spawnData
local ok = false -- A flag to say whether we found a valid X position
local gap = 2 -- Set the minimum gap in pixels between enemies
local err = 0 -- An error flag to avoid the game slowing down if too many enemies
-- EDITED CODE BELOW
--------------------
while ok == false do -- Tell it to loop until a valid X position is found
xPos = math.random(20,300) -- give a new X position
local e = enemies[#enemies] -- Get the last enemy in the table (should be the previously made)
if e == nil then
break -- Break out of the while loop
end
local dist = mAbs(e.x - xPos) -- Work out the distance between this enemy and the random position chosen
if dist >= (e.width + gap) then
ok = true
else
err = err + 1 -- Increase the count of attempts to find a valid position
if err > 50 then
ok = true
end
end
end
s.x = xPos
enemies[#enemies+1] = s
enemyGroup:insert(s)
Upvotes: 0