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

I’m facing a screen transition problem that driving me crazy…

Here is the code

[lua]function scene:createScene( event )

screenGroup = self.view

mouseGroup = display.newGroup ()
mouseGroup.anchorChildren = true
mouseGroup.anchorX = 0
mouseGroup.anchorY = 1
mouseGroup.x = 0
mouseGroup.y = 0
screenGroup:insert (mouseGroup)

wallGroup = display.newGroup ()
wallGroup.anchorChildren = true
wallGroup.anchorX = 0
wallGroup.anchorY = 1
wallGroup.x = 0
wallGroup.y = 0
screenGroup:insert (wallGroup)

pipeGroup = display.newGroup ()
pipeGroup.anchorChildren = true
pipeGroup.anchorX = 0
pipeGroup.anchorY = 1
pipeGroup.x = 0
pipeGroup.y = 0
screenGroup:insert (pipeGroup)[/lua]

I have 3 groups that contain 3 different things that i spawn for the game, which is mouse, wall, and pipes

Here is my add mouse wall pipes function[lua]function addMouse ()

mouseSheet = graphics.newImageSheet (“mouseToLeft.png”, mouseSheetInfo:getSheet() )
mouseToLeft = display.newSprite ( mouseSheet, {name = “mouseToLeft”, start = 1, count = 4, time = 175})
mouseToLeft.x = w + 100
mouseToLeft.y = h * 0.72
mouseToLeft.xScale = 0.15
mouseToLeft.yScale = 0.15
mouseToLeft.scoreAdded = false
physics.addBody (mouseToLeft, “static”, {radius = 7, density = 0.01, bounce = 0})
mouseToLeft:play ()
mouseToLeft.name = “mouse”
screenGroup:insert (mouseToLeft)
mouseGroup:insert (mouseToLeft)
screenGroup:insert(mouseGroup)

end

function addPipes ()

pipes = display.newImageRect (“pipes.png”, 30, 46)
pipes.x = w + 100
pipes.y = h * 0.7
pipes.scoreAdded = false
physics.addBody (pipes, “static”, {density = 0.01, bounce = 0})
pipes.name = “pipes”
screenGroup:insert (pipes)
pipeGroup:insert (pipes)
screenGroup:insert (pipeGroup)

end

function addWall ()

wall = display.newImageRect (“wall.png”, 15, 82)
wall.x = w + 100
wall.y = h * 0.63
wall.scoreAdded = false
wall.name = “wall”
physics.addBody (wall, “static”, { density = 0.0, bounce = 0})
screenGroup:insert (wall)
wallGroup:insert (wall)
screenGroup:insert (wallGroup)

end[/lua]

When i lose the game by colliding the player with walls,pipes,mouse

i jump to the restart screen ( restart.lua )

and instantly get an error saying that

game.lua 371 attempt to call method ‘insert’ ( a nil value )

or

game.lua 385 attempt to call method ‘insert’ ( a nil value )

or

game.lua 399 attempt to call method ‘insert’ ( a nil value )

[lua]mouseGroup:insert (mouseToLeft) <---- game.lua 371 code

pipeGroup:insert (pipes) <----------------game.lua 385 code

wallGroup:insert (wall) <------------------ game.lua 399 code[/lua]

after a few tries. 

I found out that if I don’t remove the game scene in ( function scene:enterScene (event) ) in restart.lua

[lua]function scene:enterScene( event )

storyboard.removeScene (“game”)

end[/lua]

it won’t cause the problem.

but the thing is, i have to remove the game scene right?

what can i do to solve this problem?

Perhaps you need to pause physics, remove your collision handler or remove any Runtime listeners that might still be trying to run

Rob

I have checked every game.lua’s columns

and I cannot find out the which handler that i did not remove.

[lua]

function playerJump (event)

     if (mode == 1) and (headPlayer.y > h * 0.67) and event.phase == “began” then

     headPlayer:applyForce ( 0, -20.5, headPlayer.x, headPlayer.y )

     

     end

     if (mode == 2) and (player.y > h * 0.67) and event.phase == “began” then

     player:applyForce ( 0, -5.5, player.x, player.y )

     end

end


–Transform Function


function playerTransform (event)

     if event.phase ==“began” then

          mode = 1

          player.alpha = 0

          physics.removeBody( player )

          headPlayer.alpha = 1

          physics.addBody(headPlayer, “dynamic”, { radius = 16, bounce=0, filter = headplayerCollisionFilter } )

     end

     if event.phase == “ended” then

          mode = 2

          player.alpha = 1

          physics.addBody(player, “dynamic”, { radius = 16, bounce=0, filter = playerCollisionFilter } )

          headPlayer.alpha = 0

     end

end

function transformEnd (event)

     if event.phase == “ended” then

          mode = 2

          player.alpha = 1

          physics.addBody(player, “dynamic”, { radius = 16, bounce=0, filter = playerCollisionFilter } )

          headPlayer.alpha = 0

          physics.removeBody( headPlayer )

     end

end

function playerOnCollisionWall ( self, event )

     if event.phase == “began” then

          if ( self.name == “player” and event.other.name == “wall”) then

               print (“hit wall!”) --lose game

               storyboard.gotoScene (“restart”)

          end

          if ( self.name == “headPlayer” and event.other.name == “wall”) then

               print (“lose wall!”) --break the wall

          end

          if ( self.name == “player” and event.other.name == “pipes”) then

               print (“hit pipes!”) --lose game

               storyboard.gotoScene (“restart”)

          end

          if ( self.name == “headPlayer” and event.other.name == “pipes”) then

               print (“lose pipes!”) --lose game, make sure headPlayer cant jump over pipes or too high

          end

          if ( self.name == “player” and event.other.name == “mouse”) then

               print (“hit mouse!”) --lose game

               storyboard.gotoScene (“restart”)

          end

          if ( self.name == “headPlayer” and event.other.name == “mouse”) then

               print (“lose mouse!”) --lose game, make sure headPlayer cant jump over pipes or too high

          end

     end

end

function gameStart (event)
     if event.phase == “began” then
          if gameStarted == false then
          addAllRandomTimer = timer.performWithDelay ( 1800, addRandomTimer, -1)
          moveWallTimer = timer.performWithDelay ( 20, movewall, -1)
          movePipesTimer = timer.performWithDelay ( 20, movePipes, -1)
          moveMouseTimer = timer.performWithDelay ( 20, moveMouse, -1)
          gameStarted = true
          end
     end
end

function addRandomTimer ()

     rand = math.random (3)

     if (rand < 2) then

     addWallRandomTimer = timer.performWithDelay (math.random (1000,2000), addWall, 1)

     else if (rand < 3) then

     addPipesRandomTimer = timer.performWithDelay (math.random (1000,2000), addPipes, 1)

     else if (rand < 4) then

     addMouseRandomTimer = timer.performWithDelay (math.random (1000,2000), addMouse, 1)

     end
     end
     end
end

function moveMouse ()

     for a = mouseGroup.numChildren, 1, -1 do

     if (mouseGroup[a].x < player.x ) then

          if mouseGroup[a].scoreAdded == false then

               mydata.score = mydata.score + 1

               scoreText.text = mydata.score

               mouseGroup[a].scoreAdded = true

          end

     end

     if ( mouseGroup[a].x > -100 ) then

          mouseGroup[a].x = mouseGroup[a].x - 6

     else

          mouseGroup:remove (mouseGroup[a])

          mouseGroup[a] = nil

     end

end

end

function movePipes ()

     for a = pipeGroup.numChildren, 1, -1 do

     if (pipeGroup[a].x < player.x ) then

          if pipeGroup[a].scoreAdded == false then

               mydata.score = mydata.score + 1

               scoreText.text = mydata.score

               pipeGroup[a].scoreAdded = true

     end

     end

     if ( pipeGroup[a].x > -100 ) then

          pipeGroup[a].x = pipeGroup[a].x - 6

     else

          pipeGroup:remove (pipeGroup[a])

          pipeGroup[a] = nil

     end

     end

end

function movewall ()

for a = wallGroup.numChildren, 1, -1 do

     if (wallGroup[a].x < player.x ) then

          if wallGroup[a].scoreAdded == false then

          mydata.score = mydata.score + 1

          scoreText.text = mydata.score

          wallGroup[a].scoreAdded = true

     end

     end

     if ( wallGroup[a].x > -100 ) then

          wallGroup[a].x = wallGroup[a].x - 6

     else

          wallGroup:remove (wallGroup[a])

          wallGroup[a] = nil

     end

     end

end

function addMouse ()

mouseSheet = graphics.newImageSheet (“mouseToLeft.png”, mouseSheetInfo:getSheet() )

mouseToLeft = display.newSprite ( mouseSheet, {name = “mouseToLeft”, start = 1, count = 4, time = 175})

mouseToLeft.x = w + 100

mouseToLeft.y = h * 0.72

mouseToLeft.xScale = 0.15

mouseToLeft.yScale = 0.15

mouseToLeft.scoreAdded = false

physics.addBody (mouseToLeft, “static”, {radius = 7, density = 0.01, bounce = 0})

mouseToLeft:play ()

mouseToLeft.name = “mouse”

screenGroup:insert (mouseToLeft)

mouseGroup:insert (mouseToLeft)

screenGroup:insert(mouseGroup)

end

function addPipes ()

pipes = display.newImageRect (“pipes.png”, 30, 46)

pipes.x = w + 100

pipes.y = h * 0.7

pipes.scoreAdded = false

physics.addBody (pipes, “static”, {density = 0.01, bounce = 0})

pipes.name = “pipes”

screenGroup:insert (pipes)

pipeGroup:insert (pipes)

screenGroup:insert (pipeGroup)

end

function addWall ()

wall = display.newImageRect (“wall.png”, 15, 82)

wall.x = w + 100

wall.y = h * 0.63

wall.scoreAdded = false

wall.name = “wall”

physics.addBody (wall, “static”, { density = 0.0, bounce = 0, filter = boxCollisionfilter})

screenGroup:insert (wall)

wallGroup:insert (wall)

screenGroup:insert (wallGroup)

end

[/lua]

This is all the function i have in game.lua 

[lua]function scene:enterScene( event )

storyboard.removeScene (“start”)
storyboard.removeScene (“restart”)

jump_btn:addEventListener (“touch”, playerJump)
transform_btn:addEventListener (“touch”, playerTransform)
Runtime:addEventListener (“touch”, transformEnd)

player.collision = playerOnCollisionWall
player:addEventListener (“collision”)

headPlayer.collision = playerOnCollisionWall
headPlayer:addEventListener (“collision”)

Runtime:addEventListener (“touch”, gameStart)

gameStarted = false
 

end[/lua]

This is the enterScene

[lua]function scene:exitScene( event )

display.remove (ground1)
display.remove (jump_btn)
display.remove (transform_btn)

Runtime:removeEventListener (“touch”, playerJump)
Runtime:removeEventListener (“touch”, playerTransform)
Runtime:removeEventListener (“touch”, transformEnd)

Runtime:removeEventListener (“touch”, gameStart)

player:removeEventListener (“collision”)
headPlayer:removeEventListener (“collision”)

timer.cancel (addAllRandomTimer)
timer.cancel (moveWallTimer)
timer.cancel (movePipesTimer)
timer.cancel (moveMouseTimer)

end[/lua]

And this is the exitScene

I have tried to remove everything that i could , but it doesn’t seem work

And i have another game having almost the same thing but it won’t cause any error

I think I figure it out what the problem is.

But i don’t know how to solve the problem.

     

[lua]function gameStart (event)
     if event.phase == “began” then
          if gameStarted == false then

          addAllRandomTimer = timer.performWithDelay ( 1800, addRandomTimer, -1)
          moveWallTimer = timer.performWithDelay ( 20, movewall, -1)
          movePipesTimer = timer.performWithDelay ( 20, movePipes, -1)
          moveMouseTimer = timer.performWithDelay ( 20, moveMouse, -1)

          gameStarted = true

          end
     end
end

function addRandomTimer ()
     rand = math.random (3)

          if (rand < 2) then
               addWallRandomTimer = timer.performWithDelay (math.random (1000,2000), addWall, 1)

          else if (rand < 3) then
               addPipesRandomTimer = timer.performWithDelay (math.random (1000,2000), addPipes, 1)

          else if (rand < 4) then
               addMouseRandomTimer = timer.performWithDelay (math.random (1000,2000), addMouse, 1)

          end
          end
     end
end[/lua]

I think the problem is the (addAllRandomTimer) and (addRandomTimer) function.

The addAllRandomTimer runs every 1.8 sec, and it triggers the function addRandomTimer in 1 or 2 secs.

If my player die in this 1 to 2 secs (addRandomTimer). my exit scene will remove everything in game.lua, including the all the groups.

and if the (addRandomTimer) thing going to spawn, it will have no where for them to group with.

So what can i do to solve this problem?

timer.cancel(addRandomTimer)

timer.cancel(addAllRandomTimer)

???

but the addRandomTimer is a function, and if i type

timer.cancel (addWallRandomTimer)

timer.cancel (addPipesRandomTimer)

timer.cancel (addMouseRandomTimer)

it will cause other Runtime error saids

You need to determine why those timer handles are nil?  Do you have a scope problem?  Were they declared local somewhere and then when you need to cancel them, are they not visible?

Perhaps you need to pause physics, remove your collision handler or remove any Runtime listeners that might still be trying to run

Rob

I have checked every game.lua’s columns

and I cannot find out the which handler that i did not remove.

[lua]

function playerJump (event)

     if (mode == 1) and (headPlayer.y > h * 0.67) and event.phase == “began” then

     headPlayer:applyForce ( 0, -20.5, headPlayer.x, headPlayer.y )

     

     end

     if (mode == 2) and (player.y > h * 0.67) and event.phase == “began” then

     player:applyForce ( 0, -5.5, player.x, player.y )

     end

end


–Transform Function


function playerTransform (event)

     if event.phase ==“began” then

          mode = 1

          player.alpha = 0

          physics.removeBody( player )

          headPlayer.alpha = 1

          physics.addBody(headPlayer, “dynamic”, { radius = 16, bounce=0, filter = headplayerCollisionFilter } )

     end

     if event.phase == “ended” then

          mode = 2

          player.alpha = 1

          physics.addBody(player, “dynamic”, { radius = 16, bounce=0, filter = playerCollisionFilter } )

          headPlayer.alpha = 0

     end

end

function transformEnd (event)

     if event.phase == “ended” then

          mode = 2

          player.alpha = 1

          physics.addBody(player, “dynamic”, { radius = 16, bounce=0, filter = playerCollisionFilter } )

          headPlayer.alpha = 0

          physics.removeBody( headPlayer )

     end

end

function playerOnCollisionWall ( self, event )

     if event.phase == “began” then

          if ( self.name == “player” and event.other.name == “wall”) then

               print (“hit wall!”) --lose game

               storyboard.gotoScene (“restart”)

          end

          if ( self.name == “headPlayer” and event.other.name == “wall”) then

               print (“lose wall!”) --break the wall

          end

          if ( self.name == “player” and event.other.name == “pipes”) then

               print (“hit pipes!”) --lose game

               storyboard.gotoScene (“restart”)

          end

          if ( self.name == “headPlayer” and event.other.name == “pipes”) then

               print (“lose pipes!”) --lose game, make sure headPlayer cant jump over pipes or too high

          end

          if ( self.name == “player” and event.other.name == “mouse”) then

               print (“hit mouse!”) --lose game

               storyboard.gotoScene (“restart”)

          end

          if ( self.name == “headPlayer” and event.other.name == “mouse”) then

               print (“lose mouse!”) --lose game, make sure headPlayer cant jump over pipes or too high

          end

     end

end

function gameStart (event)
     if event.phase == “began” then
          if gameStarted == false then
          addAllRandomTimer = timer.performWithDelay ( 1800, addRandomTimer, -1)
          moveWallTimer = timer.performWithDelay ( 20, movewall, -1)
          movePipesTimer = timer.performWithDelay ( 20, movePipes, -1)
          moveMouseTimer = timer.performWithDelay ( 20, moveMouse, -1)
          gameStarted = true
          end
     end
end

function addRandomTimer ()

     rand = math.random (3)

     if (rand < 2) then

     addWallRandomTimer = timer.performWithDelay (math.random (1000,2000), addWall, 1)

     else if (rand < 3) then

     addPipesRandomTimer = timer.performWithDelay (math.random (1000,2000), addPipes, 1)

     else if (rand < 4) then

     addMouseRandomTimer = timer.performWithDelay (math.random (1000,2000), addMouse, 1)

     end
     end
     end
end

function moveMouse ()

     for a = mouseGroup.numChildren, 1, -1 do

     if (mouseGroup[a].x < player.x ) then

          if mouseGroup[a].scoreAdded == false then

               mydata.score = mydata.score + 1

               scoreText.text = mydata.score

               mouseGroup[a].scoreAdded = true

          end

     end

     if ( mouseGroup[a].x > -100 ) then

          mouseGroup[a].x = mouseGroup[a].x - 6

     else

          mouseGroup:remove (mouseGroup[a])

          mouseGroup[a] = nil

     end

end

end

function movePipes ()

     for a = pipeGroup.numChildren, 1, -1 do

     if (pipeGroup[a].x < player.x ) then

          if pipeGroup[a].scoreAdded == false then

               mydata.score = mydata.score + 1

               scoreText.text = mydata.score

               pipeGroup[a].scoreAdded = true

     end

     end

     if ( pipeGroup[a].x > -100 ) then

          pipeGroup[a].x = pipeGroup[a].x - 6

     else

          pipeGroup:remove (pipeGroup[a])

          pipeGroup[a] = nil

     end

     end

end

function movewall ()

for a = wallGroup.numChildren, 1, -1 do

     if (wallGroup[a].x < player.x ) then

          if wallGroup[a].scoreAdded == false then

          mydata.score = mydata.score + 1

          scoreText.text = mydata.score

          wallGroup[a].scoreAdded = true

     end

     end

     if ( wallGroup[a].x > -100 ) then

          wallGroup[a].x = wallGroup[a].x - 6

     else

          wallGroup:remove (wallGroup[a])

          wallGroup[a] = nil

     end

     end

end

function addMouse ()

mouseSheet = graphics.newImageSheet (“mouseToLeft.png”, mouseSheetInfo:getSheet() )

mouseToLeft = display.newSprite ( mouseSheet, {name = “mouseToLeft”, start = 1, count = 4, time = 175})

mouseToLeft.x = w + 100

mouseToLeft.y = h * 0.72

mouseToLeft.xScale = 0.15

mouseToLeft.yScale = 0.15

mouseToLeft.scoreAdded = false

physics.addBody (mouseToLeft, “static”, {radius = 7, density = 0.01, bounce = 0})

mouseToLeft:play ()

mouseToLeft.name = “mouse”

screenGroup:insert (mouseToLeft)

mouseGroup:insert (mouseToLeft)

screenGroup:insert(mouseGroup)

end

function addPipes ()

pipes = display.newImageRect (“pipes.png”, 30, 46)

pipes.x = w + 100

pipes.y = h * 0.7

pipes.scoreAdded = false

physics.addBody (pipes, “static”, {density = 0.01, bounce = 0})

pipes.name = “pipes”

screenGroup:insert (pipes)

pipeGroup:insert (pipes)

screenGroup:insert (pipeGroup)

end

function addWall ()

wall = display.newImageRect (“wall.png”, 15, 82)

wall.x = w + 100

wall.y = h * 0.63

wall.scoreAdded = false

wall.name = “wall”

physics.addBody (wall, “static”, { density = 0.0, bounce = 0, filter = boxCollisionfilter})

screenGroup:insert (wall)

wallGroup:insert (wall)

screenGroup:insert (wallGroup)

end

[/lua]

This is all the function i have in game.lua 

[lua]function scene:enterScene( event )

storyboard.removeScene (“start”)
storyboard.removeScene (“restart”)

jump_btn:addEventListener (“touch”, playerJump)
transform_btn:addEventListener (“touch”, playerTransform)
Runtime:addEventListener (“touch”, transformEnd)

player.collision = playerOnCollisionWall
player:addEventListener (“collision”)

headPlayer.collision = playerOnCollisionWall
headPlayer:addEventListener (“collision”)

Runtime:addEventListener (“touch”, gameStart)

gameStarted = false
 

end[/lua]

This is the enterScene

[lua]function scene:exitScene( event )

display.remove (ground1)
display.remove (jump_btn)
display.remove (transform_btn)

Runtime:removeEventListener (“touch”, playerJump)
Runtime:removeEventListener (“touch”, playerTransform)
Runtime:removeEventListener (“touch”, transformEnd)

Runtime:removeEventListener (“touch”, gameStart)

player:removeEventListener (“collision”)
headPlayer:removeEventListener (“collision”)

timer.cancel (addAllRandomTimer)
timer.cancel (moveWallTimer)
timer.cancel (movePipesTimer)
timer.cancel (moveMouseTimer)

end[/lua]

And this is the exitScene

I have tried to remove everything that i could , but it doesn’t seem work

And i have another game having almost the same thing but it won’t cause any error

I think I figure it out what the problem is.

But i don’t know how to solve the problem.

     

[lua]function gameStart (event)
     if event.phase == “began” then
          if gameStarted == false then

          addAllRandomTimer = timer.performWithDelay ( 1800, addRandomTimer, -1)
          moveWallTimer = timer.performWithDelay ( 20, movewall, -1)
          movePipesTimer = timer.performWithDelay ( 20, movePipes, -1)
          moveMouseTimer = timer.performWithDelay ( 20, moveMouse, -1)

          gameStarted = true

          end
     end
end

function addRandomTimer ()
     rand = math.random (3)

          if (rand < 2) then
               addWallRandomTimer = timer.performWithDelay (math.random (1000,2000), addWall, 1)

          else if (rand < 3) then
               addPipesRandomTimer = timer.performWithDelay (math.random (1000,2000), addPipes, 1)

          else if (rand < 4) then
               addMouseRandomTimer = timer.performWithDelay (math.random (1000,2000), addMouse, 1)

          end
          end
     end
end[/lua]

I think the problem is the (addAllRandomTimer) and (addRandomTimer) function.

The addAllRandomTimer runs every 1.8 sec, and it triggers the function addRandomTimer in 1 or 2 secs.

If my player die in this 1 to 2 secs (addRandomTimer). my exit scene will remove everything in game.lua, including the all the groups.

and if the (addRandomTimer) thing going to spawn, it will have no where for them to group with.

So what can i do to solve this problem?

timer.cancel(addRandomTimer)

timer.cancel(addAllRandomTimer)

???

but the addRandomTimer is a function, and if i type

timer.cancel (addWallRandomTimer)

timer.cancel (addPipesRandomTimer)

timer.cancel (addMouseRandomTimer)

it will cause other Runtime error saids

You need to determine why those timer handles are nil?  Do you have a scope problem?  Were they declared local somewhere and then when you need to cancel them, are they not visible?