OOP - Can't remove enterFrame event

So I am making a car dodging game and I was redoing the spawn system when I encountered this problem. I tried to debug it but had no success. So I looked online and through the forums and found that this issue isn’t very popular. I want to make it so when an obstacle reaches a certain y value it allows another obstacle to spawn. This will allow me to slowly increase the difficulty by making cars spawn more frequently and by making sure the player has enough space to fit through. With this code I get an error saying attempting to compare numbers with nil. Meaning that it did not remove the listener. 

Here is the code with the issue

[lua]function Cars:enterFrame()
if self.cars.y > spawnLine then
carsAlive = carsAlive - 1
end
end

function Cars:moveCar()
function destroyCar()
self.cars:removeEventListener(“enterFrame”, self)
self.cars:removeSelf()
end
Runtime:addEventListener(“enterFrame”, self);
transition.to( self.cars, { time=self.velocity, x=self.cars.x, y=550, onComplete=destroyCar} )
end[/lua]

I don’t know if anyone knows what is wrong and can help me fix it or another way I can go about making a good spawning system 

Thanks

You haven’t provided enough code to understand fully what’s happening in your code but I’ll make some assumptions and hopefully that will answer your question.

Runtime:addEventListener(“enterFrame”, self); and self.cars:removeEventListener(“enterFrame”, self) are not adding and removing the enterFrame listener to the same object. Runtime is a different object from self.cars so there is no enterFrame listener to remove from self.cars.

Check out these links for more info on listeners:

Function and Table Listeners

Tutorial: Animation with “enterFrame” listeners

You haven’t provided enough code to understand fully what’s happening in your code but I’ll make some assumptions and hopefully that will answer your question.

Runtime:addEventListener(“enterFrame”, self); and self.cars:removeEventListener(“enterFrame”, self) are not adding and removing the enterFrame listener to the same object. Runtime is a different object from self.cars so there is no enterFrame listener to remove from self.cars.

Check out these links for more info on listeners:

Function and Table Listeners

Tutorial: Animation with “enterFrame” listeners