attempt to compare with nil number?

I am getting an error saying ‘attempt to compare with nil number’ 

The error is on the second line where it says if self.x <-160

this is also in my create Scene

      local function backgroundScroll(self,event)

          if self.x < -160 then

              self.x = 480

          else

              self.x = self.x - self.speed

          end

      end

    

      background.enterFrame=backgroundScroll

      Runtime:addEventListener(“enterFrame”, background)

    

      background1.enterFrame=backgroundScroll

      Runtime:addEventListener(“enterFrame”, background1)

also where do I remove these eventListeners???

Hey,

Have you checked if self is nil?

You remove the event listeners when you are done using them.  You will manually need to remove the Runtime listeners that you are using.

Rich

how and where do I remove these event listeners?

Have you looked in the docs dude? http://docs.coronalabs.com/api/type/EventListener/removeEventListener.html

You can remove them using the above call whenever you no longer need them.  So, if you’re using storyboard API then you can remove them via a call to removeEventListener() in the exitScene() method if you no longer need them once your scene exits.  You can put that call into a conditional statement to check whether the level has ended, if it has then remove the event listener - endless possibilities really.  Check out the URL above.

Rich

I removed them in the exit scene and I get an error saying  Attempt to index global ‘background’ (a nil value) 

this is what I put in my exit scene

background:removeEventListener()

background1:removeEventListener()

If you have this in your enter scene:

Runtime:addEventListener(“enterFrame”, background)

…then you want this in your exit scene:

Runtime:removeEventListener(“enterFrame”, background)

Jay

Hey,

Have you checked if self is nil?

You remove the event listeners when you are done using them.  You will manually need to remove the Runtime listeners that you are using.

Rich

how and where do I remove these event listeners?

Have you looked in the docs dude? http://docs.coronalabs.com/api/type/EventListener/removeEventListener.html

You can remove them using the above call whenever you no longer need them.  So, if you’re using storyboard API then you can remove them via a call to removeEventListener() in the exitScene() method if you no longer need them once your scene exits.  You can put that call into a conditional statement to check whether the level has ended, if it has then remove the event listener - endless possibilities really.  Check out the URL above.

Rich

I removed them in the exit scene and I get an error saying  Attempt to index global ‘background’ (a nil value) 

this is what I put in my exit scene

background:removeEventListener()

background1:removeEventListener()

If you have this in your enter scene:

Runtime:addEventListener(“enterFrame”, background)

…then you want this in your exit scene:

Runtime:removeEventListener(“enterFrame”, background)

Jay