Taylor Brown
Taylor Brown

Reputation: 13

Repeating explosions in LOVE2D

I'm using the following code:

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

And whenever an enemy is killed, it repeats the explosion 4 times. I use a similar function in a code I use for smoke, but I can change the time without using multiple variables. I'm fairly certain the error is in a number value, Can anyone tell me how to stop the problem?

Upvotes: 1

Views: 152

Answers (1)

Colonel Thirty Two
Colonel Thirty Two

Reputation: 26589

Don't use table.remove while iterating over a table; you might skip elements of the table or process elements more than once.

Set the entry to nil instead.

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

Upvotes: 1

Related Questions