I need help with destroying enemy on collision

game looks good, i didn’t realise you’d down so much already!

ok i found one problem for a start… i put this in your eggCollision function

[lua]if(dudes22[event.target]==self) then
print(“one of the dudes is self”)
end [/lua]

and discovered that that condition actually occurs.

now you’ve called [lua]dudes22[event.target]:removeSelf()
dudes22[event.target] = nil[/lua]

but after that you call
[lua]self:removeEventListener( “postCollision”, self )[/lua]

so since i already determined that one of the dudes == self, and since you’ve already removed the dude it’s not possible to remove the listener from it! you need to remove the listener first before removing the dude

however since you’re already in the collision function for that dude at the time, i really doubt you should remove that collision function then. if you remove it from itself half way through, what happens? i can’t say for sure, but it sounds like it’ll break

so this is the fix i came up with

[lua] local function onEggCollision ( self, event )

– uses “postSolve” event to get collision force

print( "force: " … event.force )

– Crack this egg if collision force is high enough
if ( event.force >=0 ) then
for dude22, v in pairs(dudes22) do

if(dudes22[event.target]~=self) then
dudes22[event.target]:removeSelf()
dudes22[event.target] = nil

end

end

score = score + 10
scoreNum.text = score

– After this egg cracks, it can ignore further collisions
self:removeEventListener( “postCollision”, self )

– self isn’t removed tho
– this crashses
–dudes22[self]=nil
–self:removeSelf()

end
end[/lua]
I will see if i can find further issues, but that should get you started.

i would suggest your sort your tab spacing in your code though, it’s almost impossible to read!!

regards
j

NOTE: i’ve updated the above post. one of the problems i think is trying to remove “self” in its own collision. In the code above if i uncomment those line at the bottom it’ll crash.

in general I would mark an item as .dead or add it to deadItems[] during the collision event and then remove these items during the enterFrame event
[import]uid: 6645 topic_id: 7257 reply_id: 28320[/import]

from what i can tell, every time you throw a brick (fire a bullet) you run the startListening function, this then checks through all the bricks and finds one that hasn’t had a postCollision defined yet. then it creates the eggCollision function, but then adds it to *every* brick again anyway. (all this happens inside startListening)

If that’s right, this is not a good approach… you dont want to keep adding listeners to all the dude22’s, every time you fire a single bullet. I would define the eggCollision function outside of the startListening function and only assign the listener to a single dude22 (dude22 is a bullet right?) at a time [import]uid: 6645 topic_id: 7257 reply_id: 28377[/import]