重复的爆炸在LOVE2D中

我使用以下代码:

local BGexplosions = {}
local image = love.graphics.newImage("textures/explosion.png")

function startBGExplosion( x, y, magn )
  table.insert(BGexplosions, {x = x, y = y, magn = magn, t = 0})
end

function drawBGExplosions()
  for k, ex in pairs(BGexplosions) do
    local sx = (ex.t/(ex.magn))
    local sy = (ex.t/(ex.magn))
    love.graphics.setColor( 255, 255, 255, 255*(1 - (ex.t/(ex.magn))) )
    local ssx = 0.5 + (sx/2)
    local ssy = sy

    love.graphics.draw( image, ex.x - (256*ssx*0.5), ex.y - (256*ssy), 0, ssx, sst, 0, 0)

    love.graphics.setColor( 255, 255, 255, 180*(1-(ex.t/(4*ex.magn))) )
    love.graphics.circle( "fill", ex.x, ex.y, 2048*(ex.t/(4*ex.magn)), 32)
  end
end

function updateBGExplosions(dt)
  for k, ex in pairs(BGexplosions) do
    ex.t = ex.t + dt
    if ex.t > 4*ex.magn then
      BGexplosions[k] = nil
    end
  end
end

每当敌人被杀死时,它就会重复4次爆炸。我在我用于烟雾的代码中使用类似的函数,但我可以改变时间而不使用多个变量。我很确定错误出现在数字值中,有人可以告诉我如何停止这个问题吗?

点赞
用户646619
用户646619

不要在迭代表时使用 table.remove,否则可能会跳过表中的元素或对元素进行多次处理。

而是应该将对应的元素设置为 nil

if ex.t > 4*ex.magn then
    BGexplosions[k] = nil
end
2015-10-19 17:25:53