My goal is to code a little shoot them up. I need help because one of my fonction doesn’t work as I expect.
When a shoot hit an alien ship, for each ship hit i want to play an explosion animation. The problem is, when i shoot others alien ships, the frames of all explosions animations reset and doesn't play independently.
Video of the problem : https://vimeo.com/manage/videos/666469977
Here is below a short version of my code with the problem. For testing i just generates explosion while pressing the « space key » randomly placed. The explosion animations doesn’t continu independently. As soon as a new explosion is generated all the animation frames played reseting !
Thanks by advance for your helps !
Code: Select all
width = love.graphics.getWidth()
height = love.graphics.getHeight()
cooldown = 0
----------- explosion
explosion = {}
explosion.tx = 64
explosion.ty = 64
explosion.sprite_sheet = love.graphics.newImage("assets/exp2_0.png")
explosion.xline =0
explosion.yline = 0
explosion.sprite = love.graphics.newQuad(explosion.xline, explosion.yline, explosion.tx, explosion.ty, explosion.sprite_sheet: getDimensions())
explosion.anim_timer = 0.09
explosion.frame = 0
explosion.max_frame = 3
------------------
tableFrameXexplosion = {0,64,128,192,0,64,128,192,0,64,128,192,0,64,128,192}
tableFrameYexplosion = {0,0,0,0,64,64,64,64,128,128,128,128,192,192,192,192}
ListOfExplosion = {}
function love.draw()
------- explosions
for i,v in ipairs(ListOfExplosion) do
love.graphics.draw(explosion.sprite_sheet, v.spriteExplo, v.x, v.y)
end
end
function love.update(dt)
Shoot(dt)
Explose(dt)
end
function Explose(dt)
for i,v in pairs(ListOfExplosion)do
if v.Explo_frame > 15 then
table.remove(ListOfExplosion, i)
end
local b = v.Explo_frame
b = math.floor(b)
v.Explo_xline = tableFrameXexplosion[b]
v.Explo_yline = tableFrameYexplosion[b]
v.Explo_frame = v.Explo_frame + explosion.anim_timer
if v.Explo_xline or v.Explo_yline ~= nil then
v.spriteExplo : setViewport(v.Explo_xline, v.Explo_yline, 64, 64)
end
end
end
function Shoot(dt)
if love.keyboard.isDown ("space") and cooldown == 0 then
local ex = love.math.random (5, 1020)
local ey = love.math.random (5, 760)
table.insert(ListOfExplosion, createExplosion(explosion.sprite,ex, ey,0,0,0))
cooldown = 25
end
if cooldown > 0 then
cooldown = math.floor (cooldown - 1 * dt)
end
end
function createExplosion(spriteExplo, a, b, Exploxline,Exployline,Exploframe)
explod = {}
explod.spriteExplo = spriteExplo
explod.x = a
explod.y = b
explod.Explo_xline = Exploxline
explod.Explo_yline = Exployline
explod.Explo_frame = Exploframe
return explod
end
https://opengameart.org/sites/default/files/exp2_0.png