In my game I am working on, I recently cleaned up a lot of my code, but all the functionality should remain the same, just performance is improved. A strange error has appeared this time. Basically when a collision occurs between a bullet and an enemy, I remove the bullet, get the (x,y) coords of the enemy, remove the enemy, then I load up an explosion animation at those (x,y) coords I stored in variables.
The animation lasts for 0.1s so what I did was inside the collision code, I used a timer, to wait 0.1s and then remove the explosion from memory so it is seen.
The collision works fine, but the timer doesn’t wait 0.1s and then call the function asked, it acts instantaneously.
I’ll show the code here:
local function explody(exp,shExp)
print( "in explody" );
exp:removeSelf();
exp = nil;
-- These two lines free texture from memory.
shExp:dispose();
shExp = nil;
end
function onBulletCollision( self, event )
if(event.phase == "began") then
-- Use an elseif here, so it hits the enemy OR it goes off screen for a second and is removed. Don't want to be able to shoot enemies off screen.
-- Could use another if here if have different types of enemies.
-- To free up memory. Remove bullet when off screen.
if(event.other.myName == "met") then
print( self.myName .. ": collision began with " .. event.other.myName ); -- Collision testing ok. (Remove on release).
self:removeSelf(); -- Remove self (bullet).
self = nil; -- Extra cautious.
xCoord = event.other.x; yCoord = event.other.y; -- Get coordinates of meteor.
event.other:removeSelf(); -- Remove the meteor from memory.
event.other = nil;
print( xCoord .. " " .. yCoord ); -- Check ok. (Remove on release).
-- Once enemy is gone, create explosion at coordinate. This happens for every enemy.
require "sprite";
local sheetExplosion = sprite.newSpriteSheet( "Media/Images/explosionSheet.png", 99,119);
local spriteSetExplosion = sprite.newSpriteSet(sheetExplosion, 1, 3); -- three frames.
sprite.add( spriteSetExplosion, "expl", 1, 3, 100,0); -- play 3 frames once. 0.1s animation.
local explosion = sprite.newSprite( spriteSetExplosion ); -- Load explosion image.
explosion.x = xCoord; explosion.y = yCoord; -- Position at the (x,y) coordinates of meteor.
explosion:prepare("expl"); -- Load animation.
explosion:play(); -- Play animation.
timer.performWithDelay(100, explody(explosion,sheetExplosion), 1); -- Give object time to animate. Pass newly created explosion object to explody function.
end
else if(event.other.myName == "ceiling") then
print( self.myName .. ": collision began with " .. event.other.myName ); -- Collision testing ok.
self:removeSelf();
self = nil;
end
end
end
I know that the function is being called, because of the print statement inside the function, but the problem is that it is being called as soon as the collision occurs, it does not wait for 0.1s and then fire the function.
This code is implemented the same way as my original code, which worked perfectly, so this really puzzles me as to why it doesn’t work.
Any help would be greatly appreciated. [import]uid: 116225 topic_id: 20848 reply_id: 320848[/import]
