I have an error in my game which I have spent countless hours trying to resolve with no success. When an enemy is destroyed I spawn an explosion object on it's position, an animated object, which I leave for 100ms to animate before I remove the explosion object. The problem is the following: If I have 2 enemies being destroyed simultaneously (or close to), which is a possibility in my game, I get an index global explosion < a nil value > error. So basically I can't have 2 or more explosion objects on screen at once, I have to wait between them until each finishes.
My current way to resolve this issue is that when an enemy explosion object is created, I continously, in an enter frame, check if an explosion object exists, and when it does I set a flag to true else set it to false. I then use this flag to run a while loop in the enemy explosion function so that it does nothing when the flag is true and then when it is false I create the next explosion and so on.
Here is my function activated on enterFrame:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | local function movePlayer(event) plane:setLinearVelocity(hspeed,0); if(plane.x > 423) then plane.x = 422; hspeed = 0; end if(plane.x < 60) then plane.x = 61; hspeed = 0; end if(explosion) then inExplode = true; else inExplode = false; end end |
You may want to give them unique names; create a table -
explosion = {}
then spawn your explosion sprites like so;
explosion[#explosion] = sprite.newSprite( spriteSetExplosion )
Let me know how that goes for you.
On a related note - you only need to set up the sprite sheet, and require "sprite" once - so you can move that out of the function.
Peach :)
Hi Peach,
Thanks for telling me that I am able to move it out of the function, I want to make my games as efficient as possible.
Your table idea is great, I have now implemented it into the game as you suggested. I just used a for loop to iterate through the table in the explody() function to remove each explosion object.
1 2 3 4 5 6 7 | explody = function() print( "in explody" ); for i in pairs (explosion) do explosion[i]:removeSelf(); explosion[i] = nil; end |
Yay! Well done :)
I'm sorry, I should have considered the +1 situation - nicely done!
Peach :)