Scroller Destroy.

Hey guys I created a thread on this before but it seems to have got lost with the new look and I can no longer see it, so im creating again. 

 

The problem was getting a scroller function to destroy scroll Event Listeners as they go off stage. 

Right now im getting the “attempt to compare nil with number” error. 

 

The error occurs on line 2 according to the terminal.

 

function scrollCloud(self, event) if self.x \< -477 then display.remove(self) Runtime:removeEventListener("enterFrame", self) print("destroyed") else self.x = self.x - self.speed end end

 

For reference these are the Event listeners for scrollCloud

 

cloud1.enterFrame = scrollCloud Runtime:addEventListener("enterFrame", cloud1) cloud2.enterFrame = scrollCloud Runtime:addEventListener("enterFrame", cloud2) cloud3.enterFrame = scrollCloud Runtime:addEventListener("enterFrame", cloud3)

 

Any ideas on fixing this? 

 

Thanks 

 

 

This looks like an issue in ( a ) ordering and ( b ) how you call event listeners.

 

  1. Remove the listener first before calling display.remove - this should prevent any ordering problems. It could be as simple as that.

  2. “self” in this case should be an object, so it may be that you need to remove self.enterFrame, since that’s the specific reference to the function to remove.

This looks like an issue in ( a ) ordering and ( b ) how you call event listeners.

 

  1. Remove the listener first before calling display.remove - this should prevent any ordering problems. It could be as simple as that.

  2. “self” in this case should be an object, so it may be that you need to remove self.enterFrame, since that’s the specific reference to the function to remove.