Object being removed twice (but isn't!)

Ok,

This is getting messy  B)

Here’s what I want to do. Have an object generated. If it’s colliding remove it if not it lives!

Object is generated off a double tap so adds:

 Runtime:addEventListener (“enterFrame” , update)

function update(event)

    if (count > 0) then

    

        timer.performWithDelay(1, do_later) …

I *assume* I can’t remove an event listener whilst in an event listener hence the timer delay

local function do_later(event)

    Runtime:removeEventListener(update)

    loader:cancelBeginOrEndCollisionCallbackBetweenTags(placing.lhTag,“static”, beginend_collision)

    loader:cancelBeginOrEndCollisionCallbackBetweenTags(placing.lhTag,“fruit”, beginend_collision)

    print(placing)

    object.kill(placing)

   

end

This do_later seems to be getting called twice! (the print happens twice) even though the object is dead!

function kill(obj)

    table.remove(obj.obj_table,obj.index)

    obj:removeSelf()

    obj = nil

end

For the record my kill routine.

I can’t see an issue here but Corona spouts: “ERROR: Attempt to remove an object that’s already been removed from the stage or whose parent/ancestor group has already been removed.”

Presumably there’s something I don’t understand or have missed so any help as always appreciated

Cheers

Can you post where you’re setting up your tap handler?  Are you using tap or touch?

Also your remove should be:

Runtime:removeEventListener(“enterFrame”, update)

Hey Rob,

Thanks for the heads up on the event removal - obviously though the error still exists.

I actually have 2 listeners - because I want ‘touch’ and ‘tap’.

Am I correct though an eventlistener cannot be removed within an eventlistener callback ??

My tap handler:

  if (event.numTaps >= 2 ) then

        

        placing = object.spawn(icons.icon_will_spawn) 

     

<blah blah> :slight_smile:

        loader:registerBeginOrEndCollisionCallbackBetweenTags(placing.lhTag,“static”, beginend_collision)

        loader:registerBeginOrEndCollisionCallbackBetweenTags(placing.lhTag,“fruit”, beginend_collision)

        Runtime:addEventListener (“enterFrame” , update

function add_listener_touch()

    

    Runtime:addEventListener( “touch”, touch )

    Runtime:addEventListener( “tap”, tap )

    

end

Any ideas?

Cheers

p.s. the error now seems to be : “bad argument #-2 to ‘originalCoronaRemoveSelf’ (Proxy expected, got nil)”

First, I don’t know how useful putting both a touch and tap listener on the same object (in this case the Runtime) will be.  Tap is a specialized touch handler that simplifies doing it with touch.   Touch events are going to send both a began and ended phase, so if you’re not testing for the phase, you’re going to end up doing things twice. 

As for “originalCoronaRemoveSelf”, not sure what you’re doing there trying to override our removeSelf, but that error is indicating that it’s expecting that perhaps you’re not calling it with the :removeSelf, but .removeSelf… or something similar.

Post some code.

Can you post where you’re setting up your tap handler?  Are you using tap or touch?

Also your remove should be:

Runtime:removeEventListener(“enterFrame”, update)

Hey Rob,

Thanks for the heads up on the event removal - obviously though the error still exists.

I actually have 2 listeners - because I want ‘touch’ and ‘tap’.

Am I correct though an eventlistener cannot be removed within an eventlistener callback ??

My tap handler:

  if (event.numTaps >= 2 ) then

        

        placing = object.spawn(icons.icon_will_spawn) 

     

<blah blah> :slight_smile:

        loader:registerBeginOrEndCollisionCallbackBetweenTags(placing.lhTag,“static”, beginend_collision)

        loader:registerBeginOrEndCollisionCallbackBetweenTags(placing.lhTag,“fruit”, beginend_collision)

        Runtime:addEventListener (“enterFrame” , update

function add_listener_touch()

    

    Runtime:addEventListener( “touch”, touch )

    Runtime:addEventListener( “tap”, tap )

    

end

Any ideas?

Cheers

p.s. the error now seems to be : “bad argument #-2 to ‘originalCoronaRemoveSelf’ (Proxy expected, got nil)”

First, I don’t know how useful putting both a touch and tap listener on the same object (in this case the Runtime) will be.  Tap is a specialized touch handler that simplifies doing it with touch.   Touch events are going to send both a began and ended phase, so if you’re not testing for the phase, you’re going to end up doing things twice. 

As for “originalCoronaRemoveSelf”, not sure what you’re doing there trying to override our removeSelf, but that error is indicating that it’s expecting that perhaps you’re not calling it with the :removeSelf, but .removeSelf… or something similar.

Post some code.

Your kill function is not nilling out that object you’re passing into it.  I just ran into the same thing and didn’t understand it.

See this:

http://forums.coronalabs.com/topic/52463-passing-an-object-as-a-parameter-to-a-function-and-setting-it-to-nil/

Thanks,

Dave

Your kill function is not nilling out that object you’re passing into it.  I just ran into the same thing and didn’t understand it.

See this:

http://forums.coronalabs.com/topic/52463-passing-an-object-as-a-parameter-to-a-function-and-setting-it-to-nil/

Thanks,

Dave