remove eventlistener in same eventlistener

So can I do this?

[lua]explosion:setSequence( “explosion” )

          explosion:play()

          

          local function ExplotionListener(event)

            if (event.phase == “ended”) then

                explosion:removeEventListener( “sprite”, ExplotionListener )

                display.remove(explosion)

                explosion = nil

                ExplotionListener = nil

            end

          end

          explosion:addEventListener( “sprite”, ExplotionListener )[/lua]

I could use timer to do this, but I want to avoid timers as much as possible :stuck_out_tongue:

No errors occur, but I am just wondering if this is horrible coding :smiley:

EDIT: I just put [lua]print(explotion)[/lua] in ExplotionListener and [lua]print(ExplotionListener)[/lua] in timer when it should be nil and it prints nil alright, but it prints it like 10 times… so this is a bad way to do this… so timers then?

The big idea is that I make objects use the same explosion animation when I destroy them. And I want to be sure there won’t be memoryleaks.

That is perfectly fine, but I’d do this:

explosion:setSequence( "explosion" ) function explosion.sprite( self, event ) if (event.phase == "ended") then self:removeEventListener( "sprite" ) display.remove(self) end end explosion:addEventListener( "sprite" ) explosion:play() -- ...

That is perfectly fine, but I’d do this:

explosion:setSequence( "explosion" ) function explosion.sprite( self, event ) if (event.phase == "ended") then self:removeEventListener( "sprite" ) display.remove(self) end end explosion:addEventListener( "sprite" ) explosion:play() -- ...