在LOVE2D中重复爆炸

时间:2015-10-19 16:57:16

标签: lua 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次。我在用于冒烟的代码中使用了类似的函数,但我可以在不使用多个变量的情况下更改时间。我相当肯定错误是数字值,有谁能告诉我如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

迭代表时不要使用table.remove;您可以多次跳过表格或处理元素的元素。

将条目设置为nil

if ex.t > 4*ex.magn then
    BGexplosions[k] = nil
end