I need help with destroying enemy on collision

on collision, the entire spawned enemies on the screen are removed instead of only one.
here is the code

bricks1 = {}
local n = 0

– the bullet animation goes here
local function throwBrick2()
n = n + 1

– goes here

bricks1[n] = display.newImage(“bomb.png”, plane.x + 10 , plane.y + 2)

physics.addBody( bricks1[n],{ density=0, friction= 0, bounce=0 } )

startListening()

– remove the “isBullet” setting below to see the brick pass through cans without colliding!
bricks1[n].isBullet = true

bricks1[n].angularVelocity =0
bricks1[n]:applyForce( 5000, 0, bricks1[n].x, bricks1[n].y )

end

here is the enemy

dudes22 = {}

local function makeDude3()
dude22 = display.newImageRect(“enemyblack13.png”, 200, 200)
dude22.xScale = .5
dude22.yscale = .1
dude22.x, dude22.y = math.random(500, 600), math.random(50, 280)
– change to 50 70
dudes22[#dudes22+1] = dude22

end

local function listener3(event)
for i, v in pairs(dudes22) do
n = n + 1
physics.addBody( dudes22[i], “kinematic” ,{density=10000, friction=0, bounce=0,radius= 35} )

if dudes22[i].x <1 then
dudes22[i]:removeSelf()
dudes22[i] = nil

else

local speed = 5
dudes22[i].x = dudes22[i].x - speed
– timer.performWithDelay( 500, throwBrick1, 1 )
– startListening()

end
end
end

timer.performWithDelay(1000, makeDude3, 500)
here is collision

function startListening()
– if egg1 has a postCollision property then we’ve already started listening
– so return immediately
for k, v in next, bricks1 do

if bricks1[k].postCollision then
return
end
end

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 k, v in next, dudes22 do
dudes22[k]:removeSelf()
– table.remove(dudes22[k],k)
dudes22[k] = nil
– After this egg cracks, it can ignore further collisions
–self:removeEventListener( “postCollision”, self )

end
return true

end
end

– Set table listeners in each egg to check for collisions
for k, v in next, dudes22 do
dudes22[k].postCollision = onEggCollision
dudes22[k]:addEventListener( “postCollision”, dudes22[k] )

end

end

help please, I have been stuck with this for almost 2 weeks, before I decided to subscribe for help [import]uid: 40990 topic_id: 7257 reply_id: 307257[/import]

Post your code inside < code > tags, and put in indentation. Without indentation it is nearly impossible to read; I had to paste the code into a text editor and add indentation.

Anyway, after reading through the code I see inside the function onEggCollision() you have this loop:

for k, v in next, dudes22 do  
 dudes22[k]:removeSelf()  
 dudes22[k] = nil  
end  

That loops through all of them. So, like, don’t do that. Only call removeSelf() on the one you want to remove. [import]uid: 12108 topic_id: 7257 reply_id: 25528[/import]

Thank you for your quick response, but when i take of the loop, the object is not been destroyed [import]uid: 40990 topic_id: 7257 reply_id: 25535[/import]

I didn’t just say take out the loop, I said:

Only call removeSelf() on the one you want to remove. [import]uid: 12108 topic_id: 7257 reply_id: 25539[/import]

I still don’t understand it, maybe I left to mention that the enemy is spawned from the same source. [import]uid: 40990 topic_id: 7257 reply_id: 25561[/import]

any help here [import]uid: 40990 topic_id: 7257 reply_id: 25601[/import]

Have you tried something like

  
event.target:removeEventListener("postCollision", onEggListener)  
event.target:removeSelf()  
dudes22[event.target] = nil  
  

Instead of a loop? [import]uid: 8366 topic_id: 7257 reply_id: 25672[/import]

You can’t access dudes22[event.target] like that because the key was a number:

dudes22[#dudes22+1] = dude22  

(#dudes22+1 is a number that means “take the highest number key in dudes22 and add one to it.”)

However as I noted in Code Exchange you don’t need to use numbers for your keys; you can actually use a display object as a key in the table:
http://developer.anscamobile.com/code/tables-lua-dictionaries-and-arrays

Thus, what I would recommend is actually use “dude22” as the key in order to make it easy to find and remove the table entry later:

dudes22[dude22] = dude22 ... dudes22[event.target] = nil [import]uid: 12108 topic_id: 7257 reply_id: 25694[/import]

a little bit help
been stuck on this for ever. any work around without tables will be appreciated [import]uid: 40990 topic_id: 7257 reply_id: 26140[/import]

I love how you ignore our posts and then expect us to help you more. [import]uid: 12108 topic_id: 7257 reply_id: 26143[/import]

i replace #dudes22+1 to dude22, the objects are spawn fine but when I call dude22:removeSelf nothing happens [import]uid: 40990 topic_id: 7257 reply_id: 26148[/import]

more help here please
I have managed to remove the enemies, but the terminal keeps giving me runtime error messages. attempt to call method removeEventListener

[code]
function startListening()
– if egg1 has a postCollision property then we’ve already started listening
– so return immediately
for k, v in next, bricks1 do

if bricks1[k].postCollision then
return
end
end

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

event.target:removeSelf()
dudes22[event.target] = nil
event.target:removeEventListener(“postCollision”, dudes22[dude22])

end
– After this egg cracks, it can ignore further collisions
–dudes22[dude22]:removeEventListener( “postCollision”, dudes22[dude22] )

end
end
for dude22, v in pairs(dudes22) do
– Set table listeners in each egg to check for collisions
dudes22[dude22].postCollision = onEggCollision
dudes22[dude22]:addEventListener( “postCollision”, dudes22[dude22] )

end
end [import]uid: 40990 topic_id: 7257 reply_id: 26784[/import]

you try remove the listener from the object *after* you’ve removed the object!

remove the listener first

[import]uid: 6645 topic_id: 7257 reply_id: 26851[/import]

jmp909,
still is not functioning any assistant for fee, donation, this is really frustrating [import]uid: 40990 topic_id: 7257 reply_id: 26964[/import]

try this

[lua]local function onEggCollision ( self, event )
if ( event.force >=0 ) then
for dude22, v in pairs(dudes22) do

–remove listener from target
event.target:removeEventListener(“postCollision”, dudes22[dude22])

– remove target from dudes22 dictionary
dudes22[event.target] = nil

– destroy the target
event.target:removeSelf()

– you had this outside your loop
dudes22[dude22]:removeEventListener( “postCollision”, dudes22[dude22] )

end

end
end

end – this was in the wrong place

– you had the setup loop below inside the collision
for dude22, v in pairs(dudes22) do
dudes22[dude22].postCollision = onEggCollision
dudes22[dude22]:addEventListener( “postCollision”, dudes22[dude22] )
end[/lua] [import]uid: 6645 topic_id: 7257 reply_id: 27000[/import]

Thanks for your response, but the function is not firing at all

no errors on the termimal, no collision print force is not executing
here is the code

[code]
function startListening()
– if egg1 has a postCollision property then we’ve already started listening
– so return immediately
for k, v in next, bricks1 do

if bricks1[k].postCollision then
return
end
end
local function onEggCollision ( self, event )
if ( event.force >=0 ) then
for dude22, v in pairs(dudes22) do

–remove listener from target
event.target:removeEventListener(“postCollision”, dudes22[dude22])

– remove target from dudes22 dictionary
dudes22[event.target] = nil

– destroy the target
event.target:removeSelf()

– you had this outside your loop
dudes22[dude22]:removeEventListener( “postCollision”, dudes22[dude22] )

end

end
end

end – this was in the wrong place

– you had the setup loop below inside the collision
for dude22, v in pairs(dudes22) do
dudes22[dude22].postCollision = onEggCollision
dudes22[dude22]:addEventListener( “postCollision”, dudes22[dude22] )
end [import]uid: 40990 topic_id: 7257 reply_id: 27230[/import]

you’re not posting enough code to determine what the problem is [import]uid: 6645 topic_id: 7257 reply_id: 27490[/import]

Here is the code

[code]
bricks1 = {}
local n = 0

– the bullet animation goes here
local function throwBrick2()
n = n + 1

– goes here
–localGroup:insert(bricks1[n])
bricks1[n] = display.newImage(“bullet.png”, plane.x + 10 , plane.y + 2)
physics.addBody( bricks1[n], { density=2.0, friction=0, bounce=0,radius =5} )

– Listen to collisions, so we may know when it hits an enemy.

– remove the “isBullet” setting below to see the brick pass through cans without colliding!
–bricks1[n].isBullet = true

bricks1[n].angularVelocity =100
bricks1[n]:applyForce( 100000, 0, bricks1[n].x, bricks1[n].y )
startListening()

end

here is the enemy

dudes22 = {}

local function makeDude3()
dude22 = display.newImageRect(“enemyblack13.png”, 200, 200)
dude22.xScale = .5
dude22.yscale = .1
dude22.x, dude22.y = math.random(500, 600), math.random(50, 280)
– change to 50 70
dudes22[dude22] = dude22

end

local function listener3(event)
for i, v in pairs(dudes22) do
n = n + 1
physics.addBody( dudes22[i], “kinematic” ,{density=10000, friction=0, bounce=0,radius= 35} )

if dudes22[i].x <1 then
dudes22[i]:removeSelf()
dudes22[i] = nil

else

local speed = 5
dudes22[i].x = dudes22[i].x - speed

end
end
end

timer.performWithDelay(1000, makeDude3, 500)

here is collision

function startListening()
– if egg1 has a postCollision property then we’ve already started listening
– so return immediately
for k, v in next, bricks1 do

if bricks1[k].postCollision then
return
end
end
local function onEggCollision ( self, event )
if ( event.force >=0 ) then
for dude22, v in pairs(dudes22) do

–remove listener from target
event.target:removeEventListener(“postCollision”, dudes22[dude22])

– remove target from dudes22 dictionary
dudes22[event.target] = nil

– destroy the target
event.target:removeSelf()

– you had this outside your loop
dudes22[dude22]:removeEventListener( “postCollision”, dudes22[dude22] )

end

end
end

end – this was in the wrong place

– you had the setup loop below inside the collision
for dude22, v in pairs(dudes22) do
dudes22[dude22].postCollision = onEggCollision
dudes22[dude22]:addEventListener( “postCollision”, dudes22[dude22] )
end
This code fires except that i get error message
attempt to index field ‘?’
the score is even firing

function startListening()
– if egg1 has a postCollision property then we’ve already started listening
– so return immediately
for k, v in next, bricks1 do

if bricks1[k].postCollision then
return
end
end

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
– event.target:removeEventListener(“postCollision”, dudes22[dude22])

dudes22[event.target]:removeSelf()
dudes22[event.target] = nil

score = score + 10
scoreNum.text = score

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

end
end
for dude22, v in pairs(dudes22) do
– Set table listeners in each egg to check for collisions
dudes22[dude22].postCollision = onEggCollision
dudes22[dude22]:addEventListener( “postCollision”, dudes22[dude22] )

end
end
[import]uid: 40990 topic_id: 7257 reply_id: 27925[/import]

if there is any workaround or solution to this problem please let me know. I’m basically going crazy with this thing. willing to spend some buck for it
please [import]uid: 40990 topic_id: 7257 reply_id: 28268[/import]

i might be able to take a look at some point. either post a zip here on mediafire or whatever. or send me an email to my username at hotmail com

ta
j [import]uid: 6645 topic_id: 7257 reply_id: 28270[/import]