Attempt to compare nil with number...

Hi,

I am getting this error each time,the player loose and it switches to another scene.What is the matter?

local function gameOver (event) if event.phase == “began” then local options = { effect = “fromBottom”, time = 700, } composer.gotoScene (“levelOne”,options) composer.removeScene (“levelOne”) end end local function groundMovement (self,event) – The editor says that the error is at the first 2 lines of this function. if self.x <-365 then self.x = 823 else self.x = self.x - 3.7 end end

sand.enterFrame = groundMovement 

Runtime:addEventListener (“enterFrame”,sand)

sand2.enterFrame = groundMovement

Runtime:addEventListener (“enterFrame”,sand2)

toothFish:addEventListener(“collision”,gameOver)

purpleFish:addEventListener(“collision”,gameOver)

blowFish:addEventListener(“collision”,gameOver)

blueFish:addEventListener(“collision”,gameOver)

greenFish:addEventListener(“collision”,gameOver)

Thanks!

It would really help to for you to copy/paste the entire error message.  There will be a line number where the error occurred.  It would be helpful to know what that line number is and what line of code is responsible for generating the error.

Rob

Hi @Rob

 Here’s the error that I get,and the whole game code.As I said I get this error when my character (squid) hits one of the enemies (toothFish,blueFish,purpleFish,greenFish,blowFish).

I think I get this error,because as quick as the player hits one f the enemyes the gameOver function is called and the game goes to another scene and…I don’t know how to fix it.

Thanks!

local function groundMovement (self,event) -- Line 136 if self.x \<-365 then -- line 137 self.x = 823 else self.x = self.x - 3.7 end end

error-1.jpg

Please don’t post your entire code.  We only need to see the problem area.  If we need to see more, we will ask for it.   What is line 137?  Everyone on the forums is busy and its a lot to ask us to copy/pasted your code into our text editor to look up your line number.  This is basic trouble shooting that you need to be able to solve your problems.

Once you find out what line 137 is, identify the variables that are being used to compare to each other.   One of them is nil.  Then ask yourself why is it nil?  If you’re not sure which is nil and which isn’t, put in some print statements and print out each variable to see which one is nil.  You can see these print statements in the command window that opens with your simulator (and is the best place to copy/paste messages from instead of doing screen shots).

Once you know which variable is nil, look, back in your code and figure out why that variable is nil.

Rob

@Rob,

  Gotcha.Here’s the problem area.It was highlighted before to.

local function groundMovement (self,event) -- Line 136 if self.x \<-365 then -- line 137 self.x = 823 else self.x = self.x - 3.7 end end&nbsp;

I tried almost any solution.I explained in my previous post why and when I am getting this error message.

Bob

Okay, then self.x is nil. Now you need to look at where you are calling groundMovement.

Rob

This is how I called groundMovement function.I told you the error comes up when my player hits an enemy and it redirects the player to another level.Never had this problem when using storyboard,but when I use storyboard the game is laggy.

Thanks!

sand.enterFrame = groundMovement Runtime:addEventListener ("enterFrame",sand) sand2.enterFrame = groundMovement Runtime:addEventListener ("enterFrame",sand2)

If that’s the case, you need to remove those two eventListeners before you go to the new scene.

Rob

It would really help to for you to copy/paste the entire error message.  There will be a line number where the error occurred.  It would be helpful to know what that line number is and what line of code is responsible for generating the error.

Rob

Hi @Rob

 Here’s the error that I get,and the whole game code.As I said I get this error when my character (squid) hits one of the enemies (toothFish,blueFish,purpleFish,greenFish,blowFish).

I think I get this error,because as quick as the player hits one f the enemyes the gameOver function is called and the game goes to another scene and…I don’t know how to fix it.

Thanks!

local function groundMovement (self,event) -- Line 136 if self.x \<-365 then -- line 137 self.x = 823 else self.x = self.x - 3.7 end end

error-1.jpg

Please don’t post your entire code.  We only need to see the problem area.  If we need to see more, we will ask for it.   What is line 137?  Everyone on the forums is busy and its a lot to ask us to copy/pasted your code into our text editor to look up your line number.  This is basic trouble shooting that you need to be able to solve your problems.

Once you find out what line 137 is, identify the variables that are being used to compare to each other.   One of them is nil.  Then ask yourself why is it nil?  If you’re not sure which is nil and which isn’t, put in some print statements and print out each variable to see which one is nil.  You can see these print statements in the command window that opens with your simulator (and is the best place to copy/paste messages from instead of doing screen shots).

Once you know which variable is nil, look, back in your code and figure out why that variable is nil.

Rob

@Rob,

  Gotcha.Here’s the problem area.It was highlighted before to.

local function groundMovement (self,event) -- Line 136 if self.x \<-365 then -- line 137 self.x = 823 else self.x = self.x - 3.7 end end&nbsp;

I tried almost any solution.I explained in my previous post why and when I am getting this error message.

Bob

Okay, then self.x is nil. Now you need to look at where you are calling groundMovement.

Rob

This is how I called groundMovement function.I told you the error comes up when my player hits an enemy and it redirects the player to another level.Never had this problem when using storyboard,but when I use storyboard the game is laggy.

Thanks!

sand.enterFrame = groundMovement Runtime:addEventListener ("enterFrame",sand) sand2.enterFrame = groundMovement Runtime:addEventListener ("enterFrame",sand2)

If that’s the case, you need to remove those two eventListeners before you go to the new scene.

Rob