attempt to index global "variable name" (a nil value)

Hi everyone.

I am new to corona.

Currently i am creating a very simple physics ballon game mostly to test and experiment with the storyboard.

So, i have a welcoming screen and a game screen. At the first screen i press “start game” and moves to scene2.lua.

When the balloon hits the floor the game stops and a screen appears asking if the player wants to play again or not. The problem is with the listener i have on the “Yes” (In my code it is mentioned as “Nai”) which is a simple text variable. Note that the error pops out as soon as the game screen comes out.

I know that my problem is very simple but i can’t think of what else to try.

Here is my code if someone wants to take a look

----------------------------------------------------------------------------------------- -- -- scene2.lua -- ----------------------------------------------------------------------------------------- -- Your code here local storyboard = require "storyboard" local scene = storyboard.newScene() local button2 local function onSceneTouch( event )     if event.phase == "began" then                  storyboard.gotoScene( "scene3", "zoomInOutFadeRotate", 800  )                  return true     end end    -- function restart( event )     -- if event.phase == "began" then                  -- storyboard.gotoScene( "scene2", "zoomInOutFadeRotate", 800  )                  -- return true     -- end -- end function scene:createScene( event )     local screenGroup = self.view     local koumpia = self.view     background = display.newImage("background.jpg")     score = 0     scoreText = display.newText("Your score is: " .. score, 0,0, native.systemFontBold, 50)     scoreText:setTextColor(0,0,0)          balloon = display.newImage("balloon.png")     balloon.x=display.contentWidth\*0.5              --floor     floor = display.newImage("floor.png")     floor.y = display.contentHeight - floor.contentHeight\*0.5                    screenGroup:insert( background )     screenGroup:insert( scoreText )     screenGroup:insert( balloon )     screenGroup:insert( floor )     --screenGroup:insert( Nai )     --screenGroup:insert( Oxi )      end function scene:enterScene( event ) function moveBalloon(event)     balloon = event.target     balloon:applyLinearImpulse(0, -0.5, event.x, event.y)     score=score+1     scoreText.text= "Your score is: " .. score end function resetScore(event)      gameLost = display.newRect(-100,0,display.contentWidth+200, display.contentHeight)     gameLost:setFillColor(0,0,0)      lost = display.newText("Exases. To score sou einai: " .. score, 0, display.contentHeight\*0.5, native.systemFontBold, 50)     lost:setTextColor(128,128,128)          restart = display.newText("Thes na ksanapaikseis?", 0, display.contentHeight\*0.5+50, native.systemFontBold, 50)     restart:setTextColor(0,255,0)     Nai = display.newText("Nai", display.contentWidth\*0.25, display.contentHeight\*0.5+100, native.systemFontBold, 50)     Nai:setTextColor(255,0,0)     Oxi = display.newText("Oxi", display.contentWidth\*0.75, display.contentHeight\*0.5+100, native.systemFontBold, 50)     Oxi:setTextColor(0,0,255) end         physics = require("physics")         physics.start()         physics.setGravity(0, 9.8)         physics.addBody(balloon,{ bounce = 0.8, radius = 45, friction = 1.0})         physics.addBody(floor, "static", {friction = 1.0})     local leftWall = display.newRect(0, 0, 1, display.contentHeight)     local rightWall = display.newRect(display.contentWidth, 0, 1, display.contentHeight)     local ceiling = display.newRect(0, 0, display.contentWidth, 1) --place the walls     physics.addBody(leftWall, "static", {bounce = 0.1})     physics.addBody(rightWall, "static", {bounce = 0.1})     physics.addBody(ceiling, "static", {bounce = 0.1})          -- remove previous scene's view     storyboard.purgeScene( "scene1" )          balloon:addEventListener("touch", moveBalloon)     floor:addEventListener("collision", resetScore)     Nai:addEventListener("touch", onSceneTouch) end function scene:exitScene( event )          balloon:removeEventListener("touch", moveBalloon)     floor:removeEventListener("collision", resetScore)     Nai:removeEventListener("touch", onSceneTouch) end function scene:destroyScene( event ) end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

Here is the error message that pops…

what is line 134 of scene2.lua

the line was the listener

Nai:addEventListener(“touch”,onSceneTouch)

which is in line number 119.

I edited the first post and deleted some useless comments i had. I also edited the error picture.

I tried exporting from notepad++ with line number but i couldn’t do it. The system does not allow me to post a .lua file so i attach it as a .doc file with line numbers
 

You never create Nai until your reset function gets called, but you are trying to add a touch listener to it even thought it doesn’t exist.

I see what you mean.

Now i create “Nai” at

function scene:createScene( event )

and it works.

The problem now is that Nai is displayed from the beggining although i want it to show after the ballon colides with the floor which then triggers the resetScore function.

What i tried was the following:

at the function scene:createScene( event )
i create the Nai as following

Nai = display.newText("Nai", display.contentWidth\*0.25, display.contentHeight\*0.5+100, native.systemFontBold, 50) Nai:setTextColor(255,0,0) Nai.isVisible=false

How can i call Nai at the resetScore function and change its .isVisible state to =true from =false?

Unless it doesn’t work that way and you have another idea of how it should be done.

I tried with the .alpha=0 and =1 to make invisible and visible but i can’t make it appear again…

Yes, I would use the .isVisible properly and change it from fall to true when you need it.  The other more practical thing is to move that addEventListener to the line just below where you create it in your resetScore() function.  You should do a:

local Nai

at the top, so you can later reference it to remove it.  Or just make sure you added to the scene’s view (group) and let the system clean it up for you.

Here is the error message that pops…

what is line 134 of scene2.lua

the line was the listener

Nai:addEventListener(“touch”,onSceneTouch)

which is in line number 119.

I edited the first post and deleted some useless comments i had. I also edited the error picture.

I tried exporting from notepad++ with line number but i couldn’t do it. The system does not allow me to post a .lua file so i attach it as a .doc file with line numbers
 

You never create Nai until your reset function gets called, but you are trying to add a touch listener to it even thought it doesn’t exist.

I see what you mean.

Now i create “Nai” at

function scene:createScene( event )

and it works.

The problem now is that Nai is displayed from the beggining although i want it to show after the ballon colides with the floor which then triggers the resetScore function.

What i tried was the following:

at the function scene:createScene( event )
i create the Nai as following

Nai = display.newText("Nai", display.contentWidth\*0.25, display.contentHeight\*0.5+100, native.systemFontBold, 50) Nai:setTextColor(255,0,0) Nai.isVisible=false

How can i call Nai at the resetScore function and change its .isVisible state to =true from =false?

Unless it doesn’t work that way and you have another idea of how it should be done.

I tried with the .alpha=0 and =1 to make invisible and visible but i can’t make it appear again…

Yes, I would use the .isVisible properly and change it from fall to true when you need it.  The other more practical thing is to move that addEventListener to the line just below where you create it in your resetScore() function.  You should do a:

local Nai

at the top, so you can later reference it to remove it.  Or just make sure you added to the scene’s view (group) and let the system clean it up for you.

sorry for the long pause.

I fixed the problem.

What i did was that instead of having “Nai” appearing on the same screen but after the collision which meant the end of the game, i created a new scene to appear Nai which was called after the collision took place.

Thank you Rob Miracle for your advices. :slight_smile:

If you have time take a look here http://forums.coronalabs.com/topic/37990-apk-file-too-big-for-no-obvious-reason/

Its a new thing i encounter now…

sorry for the long pause.

I fixed the problem.

What i did was that instead of having “Nai” appearing on the same screen but after the collision which meant the end of the game, i created a new scene to appear Nai which was called after the collision took place.

Thank you Rob Miracle for your advices. :slight_smile:

If you have time take a look here http://forums.coronalabs.com/topic/37990-apk-file-too-big-for-no-obvious-reason/

Its a new thing i encounter now…