attempt to compare number with nill(When trying to play app on the device not simulator)

my game works perfect when i run it in the simulator but as soon as i build it for my device and open the app the first circle is spawned it gives me that error… on line 96… heres the code

 local spawnCircles = function() local Fall = math.random(display.contentWidth \* 0.1, display.contentWidth \* 0.9) circle[bCircle] = display.newImageRect("circle.png", 31, 31 ) circle[bCircle].x = Fall circle[bCircle].y = -70 physics.addBody( circle[bCircle], "dynamic", {bounce = 0} ) physics.setGravity( 0, 0.5) circle[bCircle].collision = onCollision circle[bCircle]:addEventListener( "collision", circle[bCircle] ) circle[bCircle].value = bCircle bCircle = bCircle + 1 end box.collision = onCollision circleTimer = timer.performWithDelay( 1000, spawnCircles, -1 ) local function onAccelerate( event ) if box.x \> display.contentWidth \* 0.1 and box.x \< display.contentWidth \* 0.9 then -- this is line 96 box.x = box.x + (event.xInstant) \* 200 end if box.x \> display.contentWidth \* 0.9 then box.x = display.contentWidth \* 0.89 end if box.x \< display.contentWidth \* 0.1 then box.x = display.contentWidth \* 0.11 end end Runtime:addEventListener( "accelerometer", onAccelerate )

the top is is the spawning circles code and when the first circle is spawned and just about to fal from the screen the error happens… but the box goes left to right just fine before that happens… any idea why? thanks

What is line 96?

I commented what it is in the onaccelerate function

Ah, I see the comment hidden way out at the end of the line…

Well given that display.contentWidth * 0.9 should return a number, then the likely cause is box.x is nil.  You need to figure out why box.x is nil (or potentially why box is nil).  I suspect you might have a scope issue where you’ve declared box local somewhere that your onAccelerate() function can’t see it.

Rob

well see i dont understand… i made a simple little test thing and it worked… heres the code

local BG = display.newImageRect("Background.png", 1080, 1920) local box = display.newImageRect("box.png", 30, 30 ) box.x = display.contentWidth \* 0.5 box.y = 415 local function onAccelerate( event ) if box.x \> display.contentWidth \* 0.1 and box.x \< display.contentWidth \* 0.9 then box.x = box.x + (event.xInstant) \* 200 end if box.x \> display.contentWidth \* 0.9 then box.x = display.contentWidth \* 0.89 end if box.x \< display.contentWidth \* 0.1 then box.x = display.contentWidth \* 0.11 end end Runtime:addEventListener( "accelerometer", onAccelerate )

i put it into a main.lua and it worked perfect… 

i just cant figure out why its doing this…(the error)

 local circle = {} local bCircle = 1 local circleTimer local box = display.newImageRect("box.png", 30, 30) physics.addBody(box, "static", {bounce = 0} ) box.x = display.contentWidth \* 0.5 box.y = 415 box.value = 1 local onCollision = function(self, event) if event.phase == "began" then local hit = self.value local other = event.other.value if other == 1 then display.remove(box) display.remove(circle[hit]) timer.cancel( circleTimer ) composer.removeScene("game") composer.gotoScene("menu") end end end local spawnCircles = function() local Fall = math.random(display.contentWidth \* 0.1, display.contentWidth \* 0.9) circle[bCircle] = display.newImageRect("circle.png", 31, 31 ) circle[bCircle].x = Fall circle[bCircle].y = -70 physics.addBody( circle[bCircle], "dynamic", {bounce = 0} ) physics.setGravity( 0, 0.5) circle[bCircle].collision = onCollision circle[bCircle]:addEventListener( "collision", circle[bCircle] ) circle[bCircle].value = bCircle bCircle = bCircle + 1 end box.collision = onCollision circleTimer = timer.performWithDelay( 1000, spawnCircles, -1 ) local function onAccelerate( event ) if box.x \> display.contentWidth \* 0.1 and box.x \< display.contentWidth \* 0.9 then box.x = box.x + (event.xInstant) \* 200 end if box.x \> display.contentWidth \* 0.9 then box.x = display.contentWidth \* 0.89 end if box.x \< display.contentWidth \* 0.1 then box.x = display.contentWidth \* 0.11 end end Runtime:addEventListener( "accelerometer", onAccelerate )

thats the code that makes it all happen? maybe someone can find my mistake? not that the simulator doesnt notice any errors… once i build the app for android and install it then the error comes up.

I figured out what the problem is… its the onCollision function… when i comment the whole thing out and build my game it works but… so i have to try and figure out whats not agreeing in that function…

ok so in the onCollision code when the circle collides with the box i want the 1) game scene to get remove so the circles and box disappear and for it to go back to the menu scene… how is it the correct way to do this?  

Since your accelerometer is still going, it will blow up a couple frames after box gets removed, when that becomes just a lowly table.

Probably the best bet is to set box to nil after you remove it, then do:

local function onAccelerate( event ) if box then -- ADD THIS if box.x \> display.contentWidth \* 0.1 and box.x \< display.contentWidth \* 0.9 then box.x = box.x + (event.xInstant) \* 200 end if box.x \> display.contentWidth \* 0.9 then box.x = display.contentWidth \* 0.89 end if box.x \< display.contentWidth \* 0.1 then box.x = display.contentWidth \* 0.11 end end end

You’re also removing only one of your circles, whereas you probably want to do the whole bunch. You could actually load all of them (and the box) into a group, then just remove that instead. If you check the docs, you’ll see that all the shape creator functions can take a group as their first argument.

If you plan to come back to the scene (and I assume you’ll eventually want to, though it looks like you’re removing it, at the moment), it’s good practice to clean up after yourself, so in addition to box, you should set your circleTimer and circle to nil (when you remove a table, all its members go away too, so you don’t have to set them one by one to nil ), after doing your removes / cancels / etc. In this way, your scene looks like new, so re-entering is no different than entering it the first time. Simply initialize all your variables in the scene’s “show” listener (just in the “did” phase, in case you haven’t done one yet; otherwise, it’ll happen twice and double up your timer and collision handler) and you’re good to go.

Thanks for the help :slight_smile: my question now is how to stop litterly my whole game.lua on collision and just go back to menu.lua… like litterly make it like game.lua never existed… is that possible?

Well, pretty much just do all those sorts of things. Consider what it looks like when you start your level, and try to put it back in that state. Sometimes, if you have dependencies, you’ll need to do these in a certain order (often, reverse order of creation).

Also, see the code snippet in my last post, in particular the “ADD THIS”. That’s very likely the problem you’re encountering as described in your other topic. It’s explained above the snippet.

can you look at this post? 

https://forums.coronalabs.com/topic/57461-attempt-to-index-upvalue-box-a-nil-value/

I did. That’s what I meant in my last paragraph.  :slight_smile:

Look at the post above this, where I provided an updated onAccelerate  function. Notice the “if box then” check.

HOLY S***!!! thanks man!! sorry for the language i never curse unless i step on a lego but this problem has been pissing me off! thanks a million!

What is line 96?

I commented what it is in the onaccelerate function

Ah, I see the comment hidden way out at the end of the line…

Well given that display.contentWidth * 0.9 should return a number, then the likely cause is box.x is nil.  You need to figure out why box.x is nil (or potentially why box is nil).  I suspect you might have a scope issue where you’ve declared box local somewhere that your onAccelerate() function can’t see it.

Rob

well see i dont understand… i made a simple little test thing and it worked… heres the code

local BG = display.newImageRect("Background.png", 1080, 1920) local box = display.newImageRect("box.png", 30, 30 ) box.x = display.contentWidth \* 0.5 box.y = 415 local function onAccelerate( event ) if box.x \> display.contentWidth \* 0.1 and box.x \< display.contentWidth \* 0.9 then box.x = box.x + (event.xInstant) \* 200 end if box.x \> display.contentWidth \* 0.9 then box.x = display.contentWidth \* 0.89 end if box.x \< display.contentWidth \* 0.1 then box.x = display.contentWidth \* 0.11 end end Runtime:addEventListener( "accelerometer", onAccelerate )

i put it into a main.lua and it worked perfect… 

i just cant figure out why its doing this…(the error)

 local circle = {} local bCircle = 1 local circleTimer local box = display.newImageRect("box.png", 30, 30) physics.addBody(box, "static", {bounce = 0} ) box.x = display.contentWidth \* 0.5 box.y = 415 box.value = 1 local onCollision = function(self, event) if event.phase == "began" then local hit = self.value local other = event.other.value if other == 1 then display.remove(box) display.remove(circle[hit]) timer.cancel( circleTimer ) composer.removeScene("game") composer.gotoScene("menu") end end end local spawnCircles = function() local Fall = math.random(display.contentWidth \* 0.1, display.contentWidth \* 0.9) circle[bCircle] = display.newImageRect("circle.png", 31, 31 ) circle[bCircle].x = Fall circle[bCircle].y = -70 physics.addBody( circle[bCircle], "dynamic", {bounce = 0} ) physics.setGravity( 0, 0.5) circle[bCircle].collision = onCollision circle[bCircle]:addEventListener( "collision", circle[bCircle] ) circle[bCircle].value = bCircle bCircle = bCircle + 1 end box.collision = onCollision circleTimer = timer.performWithDelay( 1000, spawnCircles, -1 ) local function onAccelerate( event ) if box.x \> display.contentWidth \* 0.1 and box.x \< display.contentWidth \* 0.9 then box.x = box.x + (event.xInstant) \* 200 end if box.x \> display.contentWidth \* 0.9 then box.x = display.contentWidth \* 0.89 end if box.x \< display.contentWidth \* 0.1 then box.x = display.contentWidth \* 0.11 end end Runtime:addEventListener( "accelerometer", onAccelerate )

thats the code that makes it all happen? maybe someone can find my mistake? not that the simulator doesnt notice any errors… once i build the app for android and install it then the error comes up.

I figured out what the problem is… its the onCollision function… when i comment the whole thing out and build my game it works but… so i have to try and figure out whats not agreeing in that function…