防止生成放置在不同Y位置的相同Xpos中 - Corona SDK

时间:2016-04-21 13:55:06

标签: lua corona

我似乎遇到了一个我无法正确解决并需要一些帮助的问题。 看下面的图片,我从一个名为enemies的表中产生形状,并试图检查它们的x位置是否被一个产生物占据,如果是这样的话,然后稍微向左或向右移动它以便它弹出足以击中它们,因为它们向下移动图像显示了形状堆叠在一起的问题。

Image showing the main problem, and an example of perfect spacing

Spawn功能(从游戏循环中调用)

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)

1 个答案:

答案 0 :(得分:0)

您有两段代码可以帮助解决此错误。

在第一个片段中,您正在查看已存在的每个精灵并尝试找到合适的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次错误后,它会将精灵放在任何位置。你允许这个spawn发生。尝试更大的价值。但是,这只会推迟这个问题。

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)