i'm coding a space invaders style game, i have a large group of 'triangles' at the top, a rocket at the bottom firing lasers upwards... i'm struggling with the concept of collisions and removal of objects. i'm thinking i need all my triangle to have a seperate name/id within a table(spawnedObjects)... hence the 'spawnedObjects' +1.
When the laser hits a triangle i get this output :-
element1 = 1
element2 = 1
It removes the 2 objects from display, but (Q1) do i need to remove the triangle from the table as well?
(Q2) how would i go about this? and (Q3) i'm i going about this the correct way?
startDisplay = function () for i = 1, stack [a] do -- 1 to stack[ 1 to 30] local xx = rowPosY[a] --xx = row position local triangle = display.newImageRect( "spriteFolder/set_02.png",16,16 ) --image sceneGroup:insert( triangle ) physics.addBody( triangle, "static") triangle.myName ="triangle" spawnedObjects[#spawnedObjects+1] = triangle -- Add triangle to "spawnedObjects" table
local function onGlobalCollision( event ) print( event.object1 ) --the first object in the collision print( event.object2 ) --the second object in the collision print( "element1 = "..event.element1 ) --the element (number) of the first object which was hit in the collision print( "element2 = "..event.element2 ) --the element (number) of the second object which was hit in the collision event.object1:removeSelf() event.object2:removeSelf() event.object1 = nil event.object2 = nil end Runtime:addEventListener( "collision", onGlobalCollision )