Reputation: 21
Okay so I have a game in which I use this function in a runtime listener:
local function moveenemy(target)
if(target.direction=="left") then
target:setSequence("left")
target:setLinearVelocity(-30,0)
else
target:setSequence("right")
target:setLinearVelocity(30,0)
end
end
And I use a runtime listener to pass all my game enemies as parameters like:
Runtime:addEventListener("enterFrame",function() moveenemy(enemy1) end)
Runtime:addEventListener("enterFrame",function() moveenemy(enemy2) end)
So now when enemy is dead and I need to remove its listener, how do I remove it. Apparently the following doesnt work:
Runtime:removeEventListener("enterFrame",function() moveenemy(enemy1) end)
Runtime:removeEventListener("enterFrame",function() moveenemy(enemy2) end)
Thanks.
Upvotes: 2
Views: 825
Reputation: 226
Make a table (list) with the enemies and call them in enterFrame
function. So, when enemy die, remove it from the list.
local enemies = {enemy1, enemy2}
local myListener = function( event )
for i=1, #enemies do
moveenemy(enemies[i])
end
end
Runtime:addEventListener( "enterFrame", myListener )
Upvotes: 0
Reputation: 1156
By googling a little I found out that you are probably using Corona. And from the documentation I read that you have to pass the function in addition to the event name.
This would mean that you need to name your function when you define it and then refer to it when removing the event.
For example:
function myFunction()
-- code
end
-- add function to event
Runtime:addEventListener("enterFrame", myFunction)
-- remove function from event
Runtime:removeEventListener("enterFrame", myFunction)
You could try store the functions for each enemy to the enemies or some storage that you can refer to with the enemy or hi's ID or similar. That way you could do for example
-- add function to event
enemy1.Event = function() moveenemy(enemy1) end
Runtime:addEventListener("enterFrame", enemy1.Event)
-- remove function from event
Runtime:removeEventListener("enterFrame", enemy1.Event)
enemy1.Event = nil
Upvotes: 2