防止生成物被放置在不同 Y 位置的相同 Xpos 中 - Corona SDK
Prevent spawns being placed In the same Xpos on different Y position - Corona SDK
我似乎遇到了无法正确解决的问题,需要一些帮助。
看看下面的图片,我正在从一个叫做 enemy 的 table 中生成形状,我正在尝试检查它们的 x 位置是否被一个生成物占据,如果是,然后稍微向左或向右移动它以便它弹出足以在他们向下移动时击中他们。该图显示了形状相互堆叠的问题。
生成函数(从游戏循环中调用)
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)
您有两段代码有助于此错误。
在这第一个片段中,您正在查看已存在的每个精灵并试图找到合适的 x 位置。对于许多精灵,错误变得更容易发生。尽量只看之前Sprite的x位置。
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
在下一个片段中,在出现 50 次错误后,它会将精灵放置在任何地方。您允许这种生成发生。尝试更大的值。但是,这只会推迟问题。
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
编辑:
这是我会做的。可能有错误,因为我没有测试过。不过,希望它确实能让您了解应该做什么。
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)
我似乎遇到了无法正确解决的问题,需要一些帮助。 看看下面的图片,我正在从一个叫做 enemy 的 table 中生成形状,我正在尝试检查它们的 x 位置是否被一个生成物占据,如果是,然后稍微向左或向右移动它以便它弹出足以在他们向下移动时击中他们。该图显示了形状相互堆叠的问题。
生成函数(从游戏循环中调用)
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)
您有两段代码有助于此错误。
在这第一个片段中,您正在查看已存在的每个精灵并试图找到合适的 x 位置。对于许多精灵,错误变得更容易发生。尽量只看之前Sprite的x位置。
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
在下一个片段中,在出现 50 次错误后,它会将精灵放置在任何地方。您允许这种生成发生。尝试更大的值。但是,这只会推迟问题。
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
编辑:
这是我会做的。可能有错误,因为我没有测试过。不过,希望它确实能让您了解应该做什么。
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)