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:
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
Here is my collision code, so you can see program execution flow:
function onBulletCollision( self, event )
if(event.phase == "began") then
if(event.other.myName == "met") then
print( self.myName .. ": collision began with " .. event.other.myName );
self:removeSelf();
self = nil;
xCoord = event.other.x; yCoord = event.other.y;
event.other:removeSelf();
event.other = nil;
audio.play(explode\_sound);
enemy\_Explode(xCoord,yCoord);
elseif(event.other.myName == "ceiling") then
print( self.myName .. ": collision began with " .. event.other.myName );
self:removeSelf();
self = nil;
end
end
end
This function is then called to create the explosion:
enemy\_Explode = function(xCoordinate,yCoordinate)
while(inExplode == true) do
print( "inExplode" );
end
require "sprite";
sheetExplosion = sprite.newSpriteSheet( "Media/Images/explosionSheet.png", 99,119);
spriteSetExplosion = sprite.newSpriteSet(sheetExplosion, 1, 3);
sprite.add( spriteSetExplosion, "expl", 1, 3, 100,0);
explosion = sprite.newSprite( spriteSetExplosion );
explosion.isVisible = true;
explosion.x = xCoordinate; explosion.y = yCoordinate;
explosion:prepare("expl");
explosion:play();
timer.performWithDelay(100, explody, 1);
end
Then finally this code is used to remove the explosion:
explody = function()
print( "in explody" );
explosion:removeSelf();
explosion = nil;
-- These two lines free texture from memory.
sheetExplosion:dispose();
sheetExplosion = nil;
end
I can’t see why my code doesn’t work, when I run my game, when I hit both enemies simultaneously they both turn into an explosion object, after the 100 ms one of them then vanishes but then the other remains as an animating explosion forever resulting in the explosion nil runtime error.
I also notice that the while loop is never being executed, because the print statement is never being displayed in the simulator.
My code should make it so only one explosion appears, then once it’s done do the next one, I don’t see what I’ve done wrong here.
Any help would be amazing, I am really stumped on this error. [import]uid: 116225 topic_id: 21076 reply_id: 321076[/import]
[import]uid: 52491 topic_id: 21076 reply_id: 83279[/import]