Touch RemoveSelf function disables other RemoveSelf Function temporarily

I’ve been mulling over this for a week and am not sure what to do. This ONE problem has been the bane of my existence for the longest time.

I’m spawning E_bomb1 from this code at randomn points from the top of the screen. Once it reaches the bottom it fires off E_hit, which triggers removeSelf to remove it from the sim.

I also applied removeSelf to a touch event, so that if it is touched it also disappears.

What happens is when one E_bomb1 is touched and removeSelf any of the other E_bomb1s that are on the screen at that moment freeze their transition downward. The E_bomb1s that spawn afterwards continue to do fine, it’s just these guys that were already in play when touch was triggered screw up.

I’ve read enough to know that apparently I might be applying this removeSelf to every instance of E_bomb1 when touch is triggered and thus the other E_bomb1s in play lose their direction and stop when touch is triggered. But I can’t for the life of me find out how I can make it so that only the E_bomb1 that was touched is effected.

Here is my code:

  
local \_H = display.contentHeight;  
local \_W = display.contentWidth;   
  
  
--Bomb Code Begin  
  
  
 --Enemy Bomb1 spawn  
 --Take note that "local" makes the E\_bomb variable local to one instant of it, as opposed to without local, which makes it apply  
 --to all variables.  
 function E\_bomb1Spawn()  
 local E\_bomb1  
  
 E\_bomb1 = display.newImage("E\_bomb1.png")  
 E\_bomb1.x = math.random( \_W )  
 E\_bomb1.y = -10  
 E\_bomb1.xScale = 1  
 E\_bomb1.yScale = 1  
 --Adding the E\_bomb1.height/2 covers the other half of the height when it touches the bottom.  
 transition.to( E\_bomb1, { time=3000, y= (display.contentHeight - display.screenOriginY) + E\_bomb1.height/2 , onComplete=function() E\_hit(E\_bomb1) end })  
  
  
 --\*\*\*This is the part I'm having trouble with, when E\_hit happens, it's fine. Once I touch, the E\_bomb1s on screen other than  
 --\*\*\*the one touched ceases to animate.   
 function E\_bomb1:touch(e)  
 E\_bomb1:removeSelf()  
 end  
  
 E\_bomb1:addEventListener("touch", E\_bomb1);  
  
 function E\_hit(E\_bomb1)  
 E\_bomb1:removeSelf();  
 end  
  
 end  
  
 E\_bomb1Spawn()  
  
 --SpawnTimer  
 E\_bomb1Spawner = timer.performWithDelay(1000, E\_bomb1Spawn, -1);  
  
--Bomb Code End  

I searched everywhere for a solution, probably annoyed people enough in the chatroom, and cannot find a method. Can someone PLEASE just sit me down and tell me the proper way to do it? [import]uid: 44572 topic_id: 8306 reply_id: 308306[/import]

just as a side note… there is no need for you to call

[lua]onComplete=function() E_hit(E_bomb1) end[/lua]

E_bomb1 is already passed into the E_hit function as the parameter when you do just [lua]onComplete=E_hit[/lua] in your transition.

it’s not documented but it’s parameter is the object being transitioned

[lua]function E_hit(obj) – obj is your E_bomb1 instance[/lua] [import]uid: 6645 topic_id: 8306 reply_id: 29780[/import]

your touch event should only do that in one phase. eg [lua]if(event.phase==“ended”) then event.target:removeSelf() end[/lua]

otherwise it’ll try and remove it twice and break. the reason they stop falling is because you’ve got an error. check your console output

also why create a new touch function for every bomb?.. use a local listener.

try this…
[lua]local _H = display.contentHeight;
local _W = display.contentWidth;

–Bomb Code Begin

local function E_hit(obj)
obj:removeSelf()
end

local function onBombTouch(event)

local bomb=event.target

if(event.phase==“began”) then
if(bomb.myTransition~=nil) then
transition.cancel(bomb.myTransition)
end
bomb:removeSelf()
return true
end
end

local function E_bomb1Spawn()

local E_bomb1

–E_bomb1 = display.newImage(“E_bomb1.png”)
E_bomb1 = display.newCircle(0,0,30)
E_bomb1:setFillColor(255,0,0,255)
E_bomb1.x = math.random( _W )
E_bomb1.y = -10
E_bomb1.xScale = 1
E_bomb1.yScale = 1
–Adding the E_bomb1.height/2 covers the other half of the height when it touches the bottom.
E_bomb1.myTransition = transition.to( E_bomb1, {
time=3000,
y= (display.contentHeight - display.screenOriginY) + E_bomb1.height/2 ,
onComplete=E_hit })

E_bomb1:addEventListener(“touch”, onBombTouch)

end

E_bomb1Spawn()

–SpawnTimer
E_bomb1Spawner = timer.performWithDelay(1000, E_bomb1Spawn, -1);

–Bomb Code End[/lua] [import]uid: 6645 topic_id: 8306 reply_id: 29781[/import]

Thanks for the E_hit tip.

Not entirely sure if I grasp the concept of event.phase. Am I suppose to put this into my touch function?

I put it in like this:

 function E\_bomb1:touch(e)  
 if(event.phase=="ended") then event.target:removeSelf() end  
 end  

I got back an error that said “Runtime error… attempt to index global ‘event’ (a nil value)”
[import]uid: 44572 topic_id: 8306 reply_id: 29785[/import]

in your case it would be e.target and e.phase, since you used “e” for your event name

the touch event has several phases:

“began” - user touched screen
“moved” - user moved finger
“ended” - user removed finger
“cancelled” - not sure when this fires

if you don’t check for a phase, all 4 will process. but since you’ve removed your object the first time the rest will fail [import]uid: 6645 topic_id: 8306 reply_id: 29787[/import]

I copied and pasted your code from above over my old code and everything seems to be working fine. Thank you very much for helping me out with this, it has been a long week trying to figure this hurdle out.

The reason I used the touch function to be specific to only E_bomb1, was because it was the first “type” that I wanted present, and that I wanted to eventually add multiple ones which will act differently based on if you touch them or not.

What would be the best way to make an if statement if say bomb = E_bomb1?

Sorry, not at my computer with all my stuff right now, if the answer is obviously me simply saying if bomb=E_bomb1, then please let me know :stuck_out_tongue: [import]uid: 44572 topic_id: 8306 reply_id: 29829[/import]

in your constructor
[lua]E_Bomb1.type=“E_Bomb1”[/lua]

in your touch event
[lua]local bomb = e.target
if(bomb.type == “E_Bomb1”) then[/lua]

[import]uid: 6645 topic_id: 8306 reply_id: 29876[/import]

or here’s a version closer to your original…
[lua]local _H = display.contentHeight;
local _W = display.contentWidth;

function E_bomb1Spawn()

local E_bomb1

E_bomb1 = display.newCircle(0,0,100)
E_bomb1:setFillColor(255,0,0,255)
E_bomb1.x = math.random( _W )
E_bomb1.y = -10
E_bomb1.xScale = 1
E_bomb1.yScale = 1

function E_bomb1:touch(e)
if(e.phase==“began”) then
if(self.t~=nil) then
transition.cancel(self.t)
end
self:removeSelf()
end
end

function E_bomb1:E_hit()
self:removeSelf()
end

local endY = (display.contentHeight - display.screenOriginY) + E_bomb1.height/2
E_bomb1.t = transition.to( E_bomb1, { time=3000, y=endY , onComplete=E_bomb1.E_hit})

E_bomb1:addEventListener(“touch”, E_bomb1 );

end

E_bomb1Spawn()

–SpawnTimer
E_bomb1Spawner = timer.performWithDelay(1000, E_bomb1Spawn, -1);

–Bomb Code End[/lua] [import]uid: 6645 topic_id: 8306 reply_id: 29879[/import]