Tutorial - laser won't be removed on collision although display.remove is called

Hi guys,
 
I finished the tutorial and everything went well. Then I modified some code and found a problem.

Problem:

In the simulator, when the laser hits an asteroid, it will disappear either immediately or when it hits another asteroid ( so that’s 2 asteroids in total before the laser disappears).

What I did:

  1. I put laser related code into a “laser.lua”

  2. In the “laser.lua” I created a laser:removeLaser method, in which the display.remove is called

  3. In the collision listener in “game.lua”, when a collision between a laser and an asteroid is detected, the laser:removeLaser is called. 

laser.lua:

local laser = {} laser.laserTable = {} laser.removeLaser = function(self, laserToRemove, index) if((not index) or (index \<= 0)) then for i = #self.laserTable, 1, - 1 do if(self.laserTable[i] == laserToRemove) then index = i break end end end --here 'display.remove' is called display.remove( laserToRemove ) table.remove(self.laserTable, index) laserToRemove = nil end --some other code -- return laser

game.lua:

local laser = require( "laser" ) local function findObjName(obj1, obj2, name1, name2) local result = {found = false} if(obj1.objName == name1 and obj2.objName == name2) then result.found = true result[name1] = obj1 result[name2] = obj2 elseif(obj1.objName == name2 and obj2.objName == name1) then result.found = true result[name1] = obj2 result[name2] = obj1 end return result end -- the collision listener. local function onCollision(event) if event.phase == 'began' then local obj1 = event.object1 local obj2 = event.object2 local laserAndAsteroid = findObjName(obj1, obj2, 'laser', 'asteroid') local shipAndAsteroid = findObjName(obj1, obj2, 'asteroid', 'ship') if(laserAndAsteroid.found) then --here 'laser:removeLaser' is called laser:removeLaser(laserAndAsteroid.laser) --some other code-- end end end

Sorry for the code above, I did wrap them in the “lua” “/lua” tags, why aren’t they formatted?

Rather than wrap them in <lua> tags, highlight the code and press the blue <> code button in the toolbar to wrap them in code tags.

Thank you, Appletreeman! The code format looks good now.

I finally found what is wrong. I fired two lasers at the same time, nothing is wrong with the laser-removing code. Sorry if this topic wasted anyone’s time.

Sorry for the code above, I did wrap them in the “lua” “/lua” tags, why aren’t they formatted?

Rather than wrap them in <lua> tags, highlight the code and press the blue <> code button in the toolbar to wrap them in code tags.

Thank you, Appletreeman! The code format looks good now.

I finally found what is wrong. I fired two lasers at the same time, nothing is wrong with the laser-removing code. Sorry if this topic wasted anyone’s time.