Compare 'nil' error

Hey guys, im still getting the hang of coding and Corona SDK and am stuck solving this error.

The error is “attempt to compare nil with number” and its having the problems with line 2 of the code.
The code is supposed to scroll images out of screen and destroy it.

Any ideas whats wrong and how to solve it?

[lua]function scrollCloud(self, event)
if self.x < -480 then
display.remove(self)
Runtime:removeEventListener(“enterFrame”, self, event)
print(“destroyed”)
else
self.x = self.x - self.speed
end
end [import]uid: 220607 topic_id: 37506 reply_id: 67506[/import]

On line 3 you remove “self”. If scrollCloud() gets called again, self will be destroyed and will fail. So you have to ask why am I running this again? Well it’s likely line 4 isn’t removing the listener. Without seeing your addEventListener for it, it’s likely a problem there.

Still you should always check to see if self exists before you manipulate it:

if self and self.x \< -480 then....  

[import]uid: 199310 topic_id: 37506 reply_id: 145699[/import]

I see what you mean. in my game theres multiple event listeners for scroll cloud which all go across the screen. I used ‘self’ so that it destroys what ever event passes through.

[lua]
–cloud 1 of 4 that goes by
cloud1.enterFrame = scrollCloud
Runtime:addEventListener(“enterFrame”, cloud1) [import]uid: 220607 topic_id: 37506 reply_id: 145710[/import]

If you do this:

Runtime:addEventListener("enterFrame", cloud1)  

then you must do:

Runtime:removeEventListener("enterFrame", cloud1)  

to undo it:

Runtime:removeEventListener("enterFrame", cloud1)  

and

Runtime:removeEventListener("enterFrame", self, event)  

are not the same.

[import]uid: 199310 topic_id: 37506 reply_id: 145712[/import]

That does explain all the runtime errors im getting. Thanks for the tips, I am new to coding and am learning things from the great help from this forum.

Quick question though would I need to write a separate code to destroy the event listeners for each one individually within the same function?

like this?

[lua] function scrollCloud(self, event)
if self.x < -480 then
display.remove(self)
Runtime:removeEventListener(“enterFrame”, cloud1)
Runtime:removeEventListener(“enterFrame”, cloud2)
Runtime:removeEventListener(“enterFrame”, cloud3)
print(“destroyed”)
else
self.x = self.x - self.speed
end
end
[import]uid: 220607 topic_id: 37506 reply_id: 145782[/import]

On line 3 you remove “self”. If scrollCloud() gets called again, self will be destroyed and will fail. So you have to ask why am I running this again? Well it’s likely line 4 isn’t removing the listener. Without seeing your addEventListener for it, it’s likely a problem there.

Still you should always check to see if self exists before you manipulate it:

if self and self.x \< -480 then....  

[import]uid: 199310 topic_id: 37506 reply_id: 145699[/import]

I see what you mean. in my game theres multiple event listeners for scroll cloud which all go across the screen. I used ‘self’ so that it destroys what ever event passes through.

[lua]
–cloud 1 of 4 that goes by
cloud1.enterFrame = scrollCloud
Runtime:addEventListener(“enterFrame”, cloud1) [import]uid: 220607 topic_id: 37506 reply_id: 145710[/import]

If you do this:

Runtime:addEventListener("enterFrame", cloud1)  

then you must do:

Runtime:removeEventListener("enterFrame", cloud1)  

to undo it:

Runtime:removeEventListener("enterFrame", cloud1)  

and

Runtime:removeEventListener("enterFrame", self, event)  

are not the same.

[import]uid: 199310 topic_id: 37506 reply_id: 145712[/import]

That does explain all the runtime errors im getting. Thanks for the tips, I am new to coding and am learning things from the great help from this forum.

Quick question though would I need to write a separate code to destroy the event listeners for each one individually within the same function?

like this?

[lua] function scrollCloud(self, event)
if self.x < -480 then
display.remove(self)
Runtime:removeEventListener(“enterFrame”, cloud1)
Runtime:removeEventListener(“enterFrame”, cloud2)
Runtime:removeEventListener(“enterFrame”, cloud3)
print(“destroyed”)
else
self.x = self.x - self.speed
end
end
[import]uid: 220607 topic_id: 37506 reply_id: 145782[/import]