(Contains SSK2 Calls) Trouble removing objects

Hello everyone, I am having trouble removing an object. I am currently using @roaminggamer’s radar system module. 

I have verified that the finalize listeners for the object are running. The error I get says that the x value in one of the calculations needed for the radar system is nil. However, the finalize listener should be removing everything…

Any help would be great! Also, here’s the code:

--calls asteroid's listeners and adds it to radar(in separate function) asteroid.collision = asteroidCollision asteroid.enterFrame = removeAsteroids asteroid.finalize = asteroidFinalize asteroid:addEventListener("collision") asteroid:addEventListener("finalize") listen("enterFrame", asteroid) masterM.enemyHUD.getHUD() masterM.enemyHUD.watchObject(asteroid, {1,0,0,0.5}, 10) --finalize listener ("ran" is printed for each asteroid removed) asteroidFinalize = function(self) local masterM = require "levels-management.moduleaccessor" masterM.enemyHUD.getHUD() masterM.enemyHUD.ignoreObject(asteroid) self:removeEventListener("collision") ignore("enterFrame", asteroid) print("ran") end

Is that the actual order your code is written in?  If so, that won’t work.

If that is the order you’re writing the code in,

  1. You’ve got a global listener for ‘asteroidFinalize’

  2. You are trying to assign it to asteroid.finalize before it is defined, thus are assigning nil.

As far as the error, that is probably caused by this sequence:

  1. Object collides with something and triggers your logic which destroys it.

  2. Same object is queued for an enterFrame event in the same frame.

  3. Same object is processed for enterFrame AFTER the collision code executed.

  4. Object is invalid by the time enterFrame code executes.

Easiest solution:

If your enterFrame listener looks like this:

function obj.enterFrame( self ) ... rest of your code end

and if ‘self’ is the bad object, then do this (uses SSK)

function obj.enterFrame( self ) if( not display.isValid(self) ) then return end ... rest of your code end

Also note, you do not l need to remove local listeners, just runtime listeners:

So this:

local masterM = require "levels-management.moduleaccessor" masterM.enemyHUD.getHUD() masterM.enemyHUD.ignoreObject(asteroid) self:removeEventListener("collision") ignore("enterFrame", asteroid) print("ran") end

becomes this:

local masterM = require "levels-management.moduleaccessor" masterM.enemyHUD.getHUD() masterM.enemyHUD.ignoreObject(asteroid) ignore("enterFrame", asteroid) print("ran") end

Also, I see you’re using ignore, which is OK, but SSK supplies a much more powerful and safer feature, called ignoreList

It safely ignores a list of named listeners.  Calling ignore is unsafe if you call it twice, whereas ignoreList is not.

 ignoreList( { "enterFrame" }, asteroid)

This is most handy, when the object has multiple Runtime listeners attached (previously added via listen ):

function obj.enterFrame( self ) end; listen( "enterFrame" ) function obj.accelerometer( self ) end; listen( "accelerometer" ) function obj.onScore( self ) end; listen( "onScore" ) function obj.finalize( self ) ignoreList( { "enterFrame", "accelerometer", "onScore" }, self ) end; obj:addEventListener( "finalize" )

The function is not global, rather I declared as local before the asteroid code even begins. Sorry, for not including this.

Wow…

Thanks for all the suggestions.

Turns out it was kind of another silly mistake… Sorry…

You see, I forgot to use the self keyword in the ignore “enterFrame” call so it was not going through. I am quite the klutz. 

Thanks for all the help!

Happy New Year! (At least in one day…)

Is that the actual order your code is written in?  If so, that won’t work.

If that is the order you’re writing the code in,

  1. You’ve got a global listener for ‘asteroidFinalize’

  2. You are trying to assign it to asteroid.finalize before it is defined, thus are assigning nil.

As far as the error, that is probably caused by this sequence:

  1. Object collides with something and triggers your logic which destroys it.

  2. Same object is queued for an enterFrame event in the same frame.

  3. Same object is processed for enterFrame AFTER the collision code executed.

  4. Object is invalid by the time enterFrame code executes.

Easiest solution:

If your enterFrame listener looks like this:

function obj.enterFrame( self ) ... rest of your code end

and if ‘self’ is the bad object, then do this (uses SSK)

function obj.enterFrame( self ) if( not display.isValid(self) ) then return end ... rest of your code end

Also note, you do not l need to remove local listeners, just runtime listeners:

So this:

local masterM = require "levels-management.moduleaccessor" masterM.enemyHUD.getHUD() masterM.enemyHUD.ignoreObject(asteroid) self:removeEventListener("collision") ignore("enterFrame", asteroid) print("ran") end

becomes this:

local masterM = require "levels-management.moduleaccessor" masterM.enemyHUD.getHUD() masterM.enemyHUD.ignoreObject(asteroid) ignore("enterFrame", asteroid) print("ran") end

Also, I see you’re using ignore, which is OK, but SSK supplies a much more powerful and safer feature, called ignoreList

It safely ignores a list of named listeners.  Calling ignore is unsafe if you call it twice, whereas ignoreList is not.

 ignoreList( { "enterFrame" }, asteroid)

This is most handy, when the object has multiple Runtime listeners attached (previously added via listen ):

function obj.enterFrame( self ) end; listen( "enterFrame" ) function obj.accelerometer( self ) end; listen( "accelerometer" ) function obj.onScore( self ) end; listen( "onScore" ) function obj.finalize( self ) ignoreList( { "enterFrame", "accelerometer", "onScore" }, self ) end; obj:addEventListener( "finalize" )

The function is not global, rather I declared as local before the asteroid code even begins. Sorry, for not including this.

Wow…

Thanks for all the suggestions.

Turns out it was kind of another silly mistake… Sorry…

You see, I forgot to use the self keyword in the ignore “enterFrame” call so it was not going through. I am quite the klutz. 

Thanks for all the help!

Happy New Year! (At least in one day…)