How to remove instance of object upon collision

Ive been trying for a couple weeks now to remove the instance of my projectile without it affecting all the other instances of said projectile and cant seem to figure it out… Basically when the bullet hits the target, I want that instance of the bullet to fade out, and be removed, without affecting any other bullets in the stage.
– this code doesnt really work at all, but is a general template of how I was trying to accomplish this. Thanks in advance to anyone who might shed some light on this for me!

[code]
local bullets = display.newGroup()
local n = 1
local function fireBullet(e)

if e.phase == “began” then
bullet = display.newImage(“bullet.png”)

bullet.x, bullet.y = e.x, e.y
bullets:insert(bullet)
local function moveBullet()
BulletTrans = transition.to( bullet[n], { time=2500, alpha=1, x=(1000), y=(e.y),})
end
moveBullet()
end
end

Runtime:addEventListener(“touch”, fireBullet)

local function removeBullet()
bullets[n].removeSelf()
end

local function stopBullet()
transition.cancel(BulletTrans)
transition.to(bullet, {time = 750, alpha = 0})
removeBulletTimer = timer.performWithDelay(500, removeBullet, 1)
end


local function onLocalCollision(self, event)
if (event.phase == “began”) then
timer.performWithDelay(0, stopBullet, 1)
end

target.collision = onLocalCollision
target:addEventListener( “collision”, target )
[import]uid: 28912 topic_id: 7424 reply_id: 307424[/import]

I managed to get the bullets transitioning and colliding properly, thanks!

I am still having a bit of trouble wrapping my mind around how this works though…

local function stopBullet()  
--etc  
local t=event.source -- the timer that called this  
local bullet = t.bullet -- our custom reference  
transition.to(bullet, {time = 750, alpha = 0, onComplete:removeBullet})  
--etc  
end  
   
------  
local function onLocalCollision(self, event)  
 if (event.phase == "began") then  
 if(event.other.type=="bullet") then  
 local t = timer.performWithDelay(0, stopBullet, 1)  
 t.bullet = event.other  
 end  
 end  
end  

What is this purpose of, and what information is being passed to the t variable in the function stopBullet() with this code

[code]
local t=event.source – the timer that called this
[import]uid: 28912 topic_id: 7424 reply_id: 26279[/import]

you’re referring to a specific bullet though bullet[n] which is bullet [1]

give each bullet an id when you spawn it and also make the bullet local to your function

[lua]local bullet = display.newImage(“bullet.png”)
bullet.id=n
bullet.type=“bullet”
n=n+1
etc[/lua]

also you’re trying to remove your bullet in a timer, before (500) its transition has finished (750). use an onComplete in the timer

[lua]local function removeBullet(obj)
obj:removeSelf() – obj is the bullet
end

local function stopBullet(event)
–etc
local t=event.source – the timer that called this
local bullet = t.bullet – our custom reference
transition.to(bullet, {time = 750, alpha = 0, onComplete:removeBullet})
–etc
end


local function onLocalCollision(self, event)
if (event.phase == “began”) then
if(event.other.type==“bullet”) then
local t = timer.performWithDelay(0, stopBullet, 1)
t.bullet = event.other
end
end
end

target.collision = onLocalCollision
target:addEventListener( “collision”, target )[/lua]

i’ve left you to work out how to create your original transition and cancel it. you use bullet[n] in your code but you dont have bullet as an array, so that won’t work

why do you even create moveBullet as a function inside your function and then call that function? why not just call the transition directly?

[lua]local function fireBullet(e)

if e.phase == “began” then
bullet = display.newImage(“bullet.png”)
bullet.x, bullet.y = e.x, e.y
bullets:insert(bullet)

bullet.trans = transition.to( bullet, { time=2500, alpha=1, x=(1000), y=(e.y)})
end
end[/lua]

and now trans is a property of the bullet you will be able to use this later

[lua]transition.cancel(bullet.trans)[/lua] etc [import]uid: 6645 topic_id: 7424 reply_id: 26256[/import]

sorry i’ve fixed my code above… it was meant to be

[lua]local function stopBullet(event)
local t=event.source[/lua]

t is therefore a reference to my timer (event.source is the timer), and since i set my bullet as a property of timer t with t.bullet=bullet in the first place, i can now get that reference back to the bullet that i am talking about

(another example here https://developer.anscamobile.com/forum/2010/04/12/how-do-i-use-timerperformwithdelay-call-function-parameters#comment-11544) [import]uid: 6645 topic_id: 7424 reply_id: 26288[/import]

note the two t’s reference the same thing but are separate local variables. i could have written it like this

[lua]local function stopBullet(event)
–etc
local theTimer=event.source – the timer that called this (bulletTimer below)
local bullet = theTimer.bullet – our custom reference
transition.to(bullet, {time = 750, alpha = 0, onComplete:removeBullet})
–etc
end


local function onLocalCollision(self, event)
if (event.phase == “began”) then
if(event.other.type==“bullet”) then
local bulletTimer = timer.performWithDelay(0, stopBullet, 1)
bulletTimer.bullet = event.other – event.other is the bullet
end
end
end[/lua]

an alternative method is to use a closure (an anonymous function that calls the function you want, along with any parameters)

[lua]local function stopBullet(bulletToStop)
transition.to(bulletToStop, {time = 750, alpha = 0, onComplete:removeBullet})
–etc
end


local function onLocalCollision(self, event)
if (event.phase == “began”) then
if(event.other.type==“bullet”) then
timer.performWithDelay(0, funcion() stopBullet(event.other) end, 1)
end
end
end[/lua]

[import]uid: 6645 topic_id: 7424 reply_id: 26290[/import]