Attempt to call method 'insert' (a nil value)

hello i am trying to restart my game via composer ,when game scene is changed to restart.lua it gives me this error “game.lua:54:attempt to call method ‘insert’ (a nil value)”

game.lua;

--İstek ,require local fizik = require "physics" fizik.start() --fizik.setDrawMode("hybrid") local physicsData = (require "shapedefs").physicsData(1.0) local composer = require( "composer" ) --sahne local scene = composer.newScene() function scene:create( event ) local sceneGroup = self.view --[[puan timer.performWithDelay( 2000, puanart(),-1) --Arkaplan arkaplan= display.newImage("arkaplan.jpg") sceneGroup:insert( arkaplan ) arkaplan.x=540 arkaplan.y=960 arkaplan:toBack() arkaplan2= display.newImage("arkaplan.jpg") sceneGroup:insert( arkaplan2 ) arkaplan2.x=540 arkaplan2.y=-960 arkaplan2:toBack()]] --düşman function rastgelespwn() local isimler ={"yolcu","dsn","zep"} local isim = isimler[math.random(#isimler)] local tersisim = "ters"..isim local enemy = display.newImage(isim..".png") fizik.addBody(enemy,"static",physicsData:get(isim)) enemy.x=540 enemy.y=-100 ilerigeri=math.random(1081) ilerigeri2=math.random(1081) don=ilerigeri don2=ilerigeri2 if(don-don2\>114 or don2-don\>114) then if (don\<540) then fizik.removeBody(enemy) fizik.addBody(enemy,"static",physicsData:get(tersisim)) enemy.xScale = -1 end if (don2\<540) then fizik.removeBody(enemy) fizik.addBody(enemy,"static",physicsData:get(tersisim)) enemy.xScale = -1 end end transition.to( enemy, {time=math.random(500,2500),x=don,y=2020,print("ilk "..don),onComplete=rastgelespwn} ) transition.to( enemy, {time=math.random(2500,5500),x=don,y=2020,print("iki "..don2)}) sceneGroup:insert(enemy) end timer.performWithDelay( 2000, rastgelespwn(),-1) --Roket pixelroketilk=display.newImage("roketler/pixelroketilk.png") sceneGroup:insert( pixelroketilk ) pixelroketilk.isVisible=true --pixelroketilk.myName="pixelroketilk" fizik.addBody(pixelroketilk, "dynamic",physicsData:get("pixelroketilk")) fizik.setGravity(0,0) pixelroketilk.x=540 pixelroketilk.y=1500 --duvar local duvarsol=display.newImage("roketler/duvar/kırmızı.png") sceneGroup:insert( duvarsol ) duvarsol.x=-79 duvarsol.y=960 fizik.addBody(duvarsol,"static") local duvarsag=display.newImage("roketler/duvar/kırmızı.png") sceneGroup:insert( duvarsag ) duvarsag.x=1150 duvarsag.y=960 fizik.addBody(duvarsag,"static") end --fonksiyonlar function kay (self,event) if self.y\>2890 then self.y=-945 end self.y=self.y+15 end function hareket (event) if event.phase == "began" then if event.x\<540 then transition.to(pixelroketilk,{time=1500,x=event.x-1000}) else transition.to(pixelroketilk,{time=1500,x=event.x+1000}) end end if event.phase == "ended" then --transition.cancel() end end function carp(event) pixelroketilk:removeSelf() composer.gotoScene( "restart" ) end function puanart() --Puan --local sceneGroup = self.view local puan = 0 local sayi display.newText( "0", 200, 150 , native.systemFontBold , 40 ) puan=puan+1 --sceneGroup:insert( sayi ) end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) composer.removeScene("start") composer.removeScene("restart") composer.removeHidden() --arkaplan.enterFrame=kay --Runtime:addEventListener("enterFrame",arkaplan) --arkaplan2.enterFrame=kay --Runtime:addEventListener("enterFrame",arkaplan2) Runtime:addEventListener("touch",hareket) Runtime:addEventListener("collision",carp) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) --Runtime:removeEventListener("enterFrame",arkaplan) --Runtime:removeEventListener("enterFrame",arkaplan2) Runtime:removeEventListener("touch",hareket) Runtime:removeEventListener("collision",carp) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene

restart.lua;

local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------- -- Code outside of the scene event functions below will only be executed ONCE unless -- the scene is removed entirely (not recycled) via "composer.removeScene()" -- ----------------------------------------------------------------------------------- -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen --[[bg= display.newImage("arkaplan.jpg") sceneGroup:insert( bg ) bg.x=540 bg.y=960 bg:toBack() bg2= display.newImage("arkaplan.jpg") sceneGroup:insert( bg2 ) bg2.x=540 bg2.y=-960 bg2:toBack()]] end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) composer.removeScene("game") local fail = display.newImage("roketler/fail.png") fail:toFront( ) sceneGroup:insert(fail) fail.isVisible=true fail.x=540 fail.y=960 function sahne() timer.performWithDelay( 2000, composer.gotoScene( "game" ) ,1 ) end Runtime:addEventListener("touch",sahne) --[[bg.enterFrame=kay Runtime:addEventListener("enterFrame",bg) bg2.enterFrame=kay Runtime:addEventListener("enterFrame",bg2)]] elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) Runtime:removeEventListener("tap",sahne) --Runtime:removeEventListener("enterFrame",arkaplanstart) -- Runtime:removeEventListener("enterFrame",arkaplanstart2) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene

your

timer.performWithDelay( 2000, rastgelespwn(),-1)

is looping. 

You need to cancel or pause this timer when going to other scene.

I did a quick google translate( turkish)

this should go at the top

local benimZamanlayıcı&nbsp;

–replace other timer with this

benimZamanlayıcı=timer.performWithDelay( 2000, rastgelespwn(),-1)&nbsp;

– in scene hide (will event)

if benimZamanlayıcı then timer.pause(benimZamanlayıcı) end&nbsp;

–in scene show (did event)

if benimZamanlayıcı then timer.resume(benimZamanlayıcı) end

–in scene destroy

if benimZamanlayıcı then timer.stop(benimZamanlayıcı) end

You probably want to put i instead of ı

I’ve had this same problem come up twice this week. You are adding touch and tap handlers to “Runtime”. Please do not do this. It causes problems with scenes being re-entered at weird times.

If you want to the full screen touchable please create a full screen display.newRect(), hide it, make it hit testable, layer it just above the background (needs to be the back most touchable item) and add your listeners to that object:

local backgroundTouchBox = display.newRect( display.contentCenterX, display.contentCenterY, display.actualContentWidth, display.actualContentHeight )

sceneGroup:insert( backgroundTouchBox )

backgroundTouchBox.isVisible = false

backgroundTouchBox.isHitTestable = true

backgroundTouchBox:addEventListener( “touch”, yourTouchFunction )

While adding touch handlers to Runtime might work in simple circumstances, its not working with Composer.

Rob

Thanks for all of your answers.I tried what you guys said but it didnt worked well i just removed the onComplete=rastgelespwn in the ;

transition.to( enemy, {time=math.random(500,2500),x=don,y=2020,print("ilk "..don),onComplete=rastgelespwn} )

it worked that way but now its only spawning one enemy in the game scene and i need to make it spawn until a collision or until changing the scene.

I just solved it by just simply removing the “()” in the

zaman = timer.performWithDelay( 2000, rastgelespwn(),-1)

and making it look like this;

zaman = timer.performWithDelay( 2000, rastgelespwn,-1)

your

timer.performWithDelay( 2000, rastgelespwn(),-1)

is looping. 

You need to cancel or pause this timer when going to other scene.

I did a quick google translate( turkish)

this should go at the top

local benimZamanlayıcı&nbsp;

–replace other timer with this

benimZamanlayıcı=timer.performWithDelay( 2000, rastgelespwn(),-1)&nbsp;

– in scene hide (will event)

if benimZamanlayıcı then timer.pause(benimZamanlayıcı) end&nbsp;

–in scene show (did event)

if benimZamanlayıcı then timer.resume(benimZamanlayıcı) end

–in scene destroy

if benimZamanlayıcı then timer.stop(benimZamanlayıcı) end

You probably want to put i instead of ı

I’ve had this same problem come up twice this week. You are adding touch and tap handlers to “Runtime”. Please do not do this. It causes problems with scenes being re-entered at weird times.

If you want to the full screen touchable please create a full screen display.newRect(), hide it, make it hit testable, layer it just above the background (needs to be the back most touchable item) and add your listeners to that object:

local backgroundTouchBox = display.newRect( display.contentCenterX, display.contentCenterY, display.actualContentWidth, display.actualContentHeight )

sceneGroup:insert( backgroundTouchBox )

backgroundTouchBox.isVisible = false

backgroundTouchBox.isHitTestable = true

backgroundTouchBox:addEventListener( “touch”, yourTouchFunction )

While adding touch handlers to Runtime might work in simple circumstances, its not working with Composer.

Rob

Thanks for all of your answers.I tried what you guys said but it didnt worked well i just removed the onComplete=rastgelespwn in the ;

transition.to( enemy, {time=math.random(500,2500),x=don,y=2020,print("ilk "..don),onComplete=rastgelespwn} )

it worked that way but now its only spawning one enemy in the game scene and i need to make it spawn until a collision or until changing the scene.

I just solved it by just simply removing the “()” in the

zaman = timer.performWithDelay( 2000, rastgelespwn(),-1)

and making it look like this;

zaman = timer.performWithDelay( 2000, rastgelespwn,-1)