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]
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 [import]uid: 44572 topic_id: 8306 reply_id: 29829[/import]