Local collision for a class member, handled by the class

Hi all,

I am trying to make a class( Which is a display group ) have a local collision handler.

Here are some excerpts :

------------------------------------------------------- -- pickup.lua function pickup:touched() self.active = false end function pickup.new(x,y )    -- constructor local pickupGroup = display.newGroup() local items = {} pickupGroup.items = items pickupGroup.active = true local core = display.newCircle( x,y, 10 ) core:setFillColor(pickupColor[1],pickupColor[2],pickupColor[3],0.8) pickupGroup.core = core pickupGroup:insert(core) core.parent = pickupGroup table.insert( pickupGroup.items, core)   return setmetatable( pickupGroup, pickup\_mt ) end

------------------------------------- -- gamescreen.lua local function onPickupCollision(  event )     if ( event.phase == "began" ) then         event.target.parent:touched()     elseif ( event.phase == "ended" ) then     end end local function addPickups(levelNumber,parent,globalYOffset) for i=1,#bank.levels[levelNumber].pickups do local x = bank.levels[levelNumber].pickups[i][1] local y = bank.levels[levelNumber].pickups[i][2]+globalYOffset local newPickup = Pickup.new(x,y) physics.addBody( newPickup.core , "static",{isSensor=true}) parent:insert(newPickup) newPickup.core.collision = onPickupCollision newPickup.core:addEventListener( "collision", onPickupCollision ) end end

So the pickup class actually contains several display objects, where i want to detect collisions with one of those objects ‘core’.

I want a function of that class to handle that collision.

The code above gives a nil error when executing this line 

event.target.parent:touched()

Or is there a better way to do this ?

Thanks in advance,

Damn i got the function call wrong, changed it to this 

newPickup.core.collision = onPickupCollision newPickup.core:addEventListener( "collision", newPickup.core ) 

And i use self now 

local function onPickupCollision( self, event )     if ( event.phase == "began" ) then         self.parent:touched()     elseif ( event.phase == "ended" ) then     end end

But i still get an “attempt to call touched a nil method” error when the collision happens

Ok, seems like parent is a reserved name.

If i change it to something else it seems to work.

second time this has happened to me, i got stuck on using credits.lua for a while too.

Thanks anyway, any general advice on what is in my code is also appreciated.

Damn i got the function call wrong, changed it to this 

newPickup.core.collision = onPickupCollision newPickup.core:addEventListener( "collision", newPickup.core ) 

And i use self now 

local function onPickupCollision( self, event )     if ( event.phase == "began" ) then         self.parent:touched()     elseif ( event.phase == "ended" ) then     end end

But i still get an “attempt to call touched a nil method” error when the collision happens

Ok, seems like parent is a reserved name.

If i change it to something else it seems to work.

second time this has happened to me, i got stuck on using credits.lua for a while too.

Thanks anyway, any general advice on what is in my code is also appreciated.