I have a table called ob1 which contains 10 images(these are created randomly & moved horizontally), i have a bar at the middle of screen, i have enabled physics for both & implemented collision event listener to remove ob1 image when they collide with bar but the problem is that when 3-4 images are approaching toward bar & when the first ob1 collides with bar random ob1object are getting removed but not the current ob1 object, how can i get the id of current collided ob1 object ?
local ob1={} for i=1,obslimit do ob1[i]=display.newImage( "images/ob1.png", 250,250) ob1[i].isVisible=false ob1[i].isAlive=false physics.addBody( ob1[i], "dynamic", {friction=1,bounce=0.0}) ob1[i].gravityScale=0 ob1[i].isBodyActive=false end --function to deal ob1 collision local function ob1cols( self,event ) if(event.phase=="began") then --print(self.myName..event.other.myName) local ob1\_elem = require("mydata") --ob1\_elem.new.isBodyActive=false ob1\_elem.new.isVisible=false ob1\_elem.new.isAlive=false end end --function to deal ob1 group pooling local function getob1() --calling ob1 from pool for i=1, #ob1 do if not ob1[i].isAlive then --print( "got one" ) return ob1[i] end end return nil end --function to deal obstacle spawning local function obdecide(event) if (mytime==100) then --local ob1\_elem = getob1() local ob1\_elem = require("mydata") ob1\_elem.new=getob1() if (ob1\_elem.new~=nil) then ob1\_elem.new.isVisible=true ob1\_elem.new.isAlive=true ob1\_elem.new.isBodyActive=true ob1\_elem.new.x=math.random( 200,350 ) --trying to add event listner for every object ob1\_elem.new.myName="cactus" ob1\_elem.new:setLinearVelocity( -20, 0 ) ob1\_elem.new.collision=ob1cols ob1\_elem.new:addEventListener( "collision", ob1\_elem.new) end elseif (mytime\>100) then mytime=0 end end
(it’s throwing nil reference error) , i just want to remove that partilcuar object which collided with bar at the moment, it would be great if there is a way to do that.