unkown error.

I previously had an error that the scene is not restarting when I press the play button in restart.lua .

I was told to fix that with some code and now this error is displayed:

Windows simulator build date: May 27 2015 @ 18:15:50

Copyright © 2009-2015  C o r o n a   L a b s   I n c .
        Version: 3.0.0
        Build: 2015.2646
Platform: SM-G900S / x64 / 6.2 / Intel® HD Graphics / 4.0.0 - Build 10.18.10.3
408 / 2015.2646
Loading project from:   c:\users\true\documents\corona projects\bouncy blocks
Project sandbox folder: C:\Users\True\AppData\Roaming\Corona Labs\Corona Simulat
or\Sandbox\bouncy blocks-C116AB3B868BE13E671AF11DFB4D395F\Documents
WARNING: display.setStatusBarMode() not supported in the simulator for SM-G900S
device
Runtime error
c:\users\true\documents\corona projects\bouncy blocks\level1.lua:159: attempt to
 index global ‘self’ (a nil value)
stack traceback:
        [C]: in function ‘error’
        ?: in function ‘gotoScene’
        c:\users\true\documents\corona projects\bouncy blocks\menu.lua:25: in fu
nction <c:\users\true\documents\corona projects\bouncy blocks\menu.lua:23>
        ?: in function <?:221>

level1.lua:

local composer = require( "composer" ) local scene = composer.newScene() local physics = require( "physics" ) function scene:create( event ) -- display background image local background = display.newImage( "background.png" ) -- display ground image local ground = display.newImage( "ground.png" ) ground.myName = "ground" ground.isGround = true physics.addBody( ground, "static" , { friction=0.5, bounce=0.1 } ) -- create object local myObject = display.newRect( 0, 0, 100, 30 ) myObject:setFillColor( 0 ) myObject.isMyObject = true physics.addBody( myObject, "kinematic" , { myObject, friction=0.5, bounce=2 } ) -- touch listener function function myObject:touch( event ) if event.phase == "moved" then self.x = event.x self.y = event.y end return true end -- make 'myObject' listen for touch events myObject:addEventListener( "touch", myObject ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local block = display.newImage( "block.png" ) block.rotation = 8 block.myName = "block" block.isBlock = true physics.addBody( block, "dynamic" , { block, friction=0.5, bounce=0.5 } ) local function onCollision( self, event ) local other = event.other if( event.phase == "began" and self.isBlock and other.isGround ) then timer.performWithDelay( 30, function() composer.gotoScene ( "restart", "fade", 500 ); end) self:removeEventListener( "collision" ) end return true end block.collision = onCollision block:addEventListener( "collision" ) local block2 = display.newImage( "block2.png" ) block2.rotation = 8 block2.isBlock2 = true physics.addBody( block2, "dynamic" , { block2, friction=0.5, bounce=0.5 } ) local function onCollision( self, event ) local other = event.other if( event.phase == "began" and self.isBlock2 and other.isGround ) then timer.performWithDelay( 30, function() composer.gotoScene ( "restart", "fade", 500 ); end) self:removeEventListener( "collision" ) end return true end block2.collision = onCollision block2:addEventListener( "collision" ) sceneGroup:insert(background) sceneGroup:insert(ground) sceneGroup:insert(wall) sceneGroup:insert(block) sceneGroup:insert(block2) sceneGroup:insert(myObject) end local sceneGroup = self.view function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then background.x = 10; background.y = 308 ground.x = 145; ground.y = 480 myObject.x = 145; myObject.y = 400 wall.x, wall.y = -6, 387 wall.x, wall.y = -6, 300 wall.x, wall.y = -6, 202 wall.x, wall.y = -6, 104 wall.x, wall.y = -6, 7 wall.x, wall.y = 50, -49 wall.x, wall.y = 135, -49 wall.x = 232 wall.y = -49 wall.x = 335 wall.y = -49 wall.x = 325 wall.y = 3 wall.x = 325 wall.y = 98 wall.x = 325 wall.y = 195 wall.x = 325 wall.y = 292 wall.x = 325 wall.y = 387 block.x = 200 block2.x = 100 elseif ( phase == "did" ) then physics.start() physics.setGravity(0, 40) end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then physics.pause() elseif ( phase == "did" ) then end end function scene:destroy( event ) local sceneGroup = self.view end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

thank you.

On line 159 you have

local sceneGroup = self.view

There is no self at this point in the code. Have you noticed how some functions, like composer.newScene(), use a dot whereas other functions like scene:show() use a colon ?       Basically what the : does is to make this self. So self, which is just the scene, exists inside these functions, but not outside. You need to remove line 159 as it is doing nothing other than producing the error, but you can insert it at the top of scene:create() as you won’t be able to insert your objects into the sceneGroup if you haven’t declared what it is.

A couple of more things:

You are declaring objects like background and myObject locally inside the scene:create() function, so they won’t be visible in the other composer functions. To fix this you can just remove the locals in front of them and forward declare them, somewhere before you create these functions ( like is done in your restart.lua).

Also, you need to give your walls different names (e.g. wall1, wall2, etc). Look at the following code:

local wall = "a wall" local wall = "a different wall" print(wall) print(wall)

You might expect this to print “a wall” first, and then “a different wall”, however it prints “a different wall” each time. The second line basically just overrides the first. When you say local wall = “a wall” you are basically telling the computer to find a place in memory called wall and save “a wall” there, and if there is no place called wall, then create one. So the second line finds the place you just created and replaces it with “a different wall”. When you print wall each time, then, you are just printing from the same source.

Hope this helps.

Your code is not formatted very well. Just looking at your code, you can’t see which “end” goes with which block it belongs to. If your code was formatted with each block indented, you would have easily spotted that line 159  is outside of “function scene:create()” and when that line executes, self is nil and generating your error. 

Please read this tutorial and apply its practices. It’s going to save you a ton of headaches down the line:

https://coronalabs.com/blog/2015/06/09/tutorial-the-value-of-well-formatted-code/

Many people here on the forums do not have the time to try and match up these ends and blocks they belong to. If you format your code, it will make providing support to you better.

Rob

On line 159 you have

local sceneGroup = self.view

There is no self at this point in the code. Have you noticed how some functions, like composer.newScene(), use a dot whereas other functions like scene:show() use a colon ?       Basically what the : does is to make this self. So self, which is just the scene, exists inside these functions, but not outside. You need to remove line 159 as it is doing nothing other than producing the error, but you can insert it at the top of scene:create() as you won’t be able to insert your objects into the sceneGroup if you haven’t declared what it is.

A couple of more things:

You are declaring objects like background and myObject locally inside the scene:create() function, so they won’t be visible in the other composer functions. To fix this you can just remove the locals in front of them and forward declare them, somewhere before you create these functions ( like is done in your restart.lua).

Also, you need to give your walls different names (e.g. wall1, wall2, etc). Look at the following code:

local wall = "a wall" local wall = "a different wall" print(wall) print(wall)

You might expect this to print “a wall” first, and then “a different wall”, however it prints “a different wall” each time. The second line basically just overrides the first. When you say local wall = “a wall” you are basically telling the computer to find a place in memory called wall and save “a wall” there, and if there is no place called wall, then create one. So the second line finds the place you just created and replaces it with “a different wall”. When you print wall each time, then, you are just printing from the same source.

Hope this helps.

Your code is not formatted very well. Just looking at your code, you can’t see which “end” goes with which block it belongs to. If your code was formatted with each block indented, you would have easily spotted that line 159  is outside of “function scene:create()” and when that line executes, self is nil and generating your error. 

Please read this tutorial and apply its practices. It’s going to save you a ton of headaches down the line:

https://coronalabs.com/blog/2015/06/09/tutorial-the-value-of-well-formatted-code/

Many people here on the forums do not have the time to try and match up these ends and blocks they belong to. If you format your code, it will make providing support to you better.

Rob