insert error when going back on a scene using director

hi everyone, this is my first game, it is like a 2 games in 1 type of game where i have 5 games and you can select what game you would want to play, my problem is whenever i choose a game then I press the retry button of which lets you go back to the main menu where you can select all the games and then I choose another game, it gives out an “attempt to call method ‘insert’ (a nil value)” error, the error always points out to an object in the second game of which is correct or so i think. The first game is ok even if I retry it for a million times, but when I choose the second game then there will be that errror. By the way I’m using tophermade’s menu structure master

https://github.com/tophermade/Menu-Structure, it is using director, here’s my code for the first game:

------------------------------------------------------------------------------------------------------------------------------------

first game

-------------------------------------------------------------------------------------------

module(…, package.seeall)

    

function new()    

local physics = require(“physics”)

physics.start()

display.setStatusBar( display.HiddenStatusBar )

physics.setScale( 60 ) – a value that seems good for small objects (based on playtesting)

physics.setGravity( 0, 0 )

    level_name = “Friendly Level Name”

    level_id = “level1” --should match lua file

    

    – we store everything inside this group at the end

    local localGroup = display.newGroup()        

    

    – change scene function

    function changeScene(e)

        if(e.phase == “ended”) then

        

            local path = system.pathForFile( “loadlast.txt”, system.DocumentsDirectory )

            – io.open opens a file at path. returns nil if no file found

            local file = io.open( path, “r” )

            if file then

               file = io.open( path, “w” )

               file:write(level_id)

               io.close( file )

            else

               – create file b/c it doesn’t exist yet

               file = io.open( path, “w” )

               file:write(level_id)

               io.close( file )

            end

        

            director:changeScene(e.target.scene, “moveFromRight”);

        end

    end

    


    – we want the game object to be available here

    – level & game code

----    

    --game = require(“credit”)

    


    – //we want the game object to be available here

    – //level & game code

----        

    --local cred = display.newImage( “credit.png”)

    – pop up menu with options to reset, load main menu, etc

    local level_menu = display.newGroup()

        level_menu:setReferencePoint(display.CenterReferencePoint);

        level_menu.alpha = 0;

            transition.to(level_menu, {delay=200, time=500, alpha=1});    

        

    

        

        

-----------------------------------------------------------------------------------------------------------------    

 – overhead view, therefore no gravity vector

local tabletop = display.newImageRect( “marb/ground2.png”,500,570) – “true” overrides Corona’s downsizing of large images on smaller devices

tabletop.x = 250

tabletop.y = 0

level_menu:insert(tabletop);

local ho = display.newImageRect( “marb/hole.png”,30,30) 

ho.x = 450

ho.y = 150

level_menu:insert(ho);

local endBumperShape = { -212,-18, 212,-18, 190,2, -190,2 }

local bumper1 = display.newImageRect( “marb/bumper_end.png”,1500,20 )–bumper1.isVisible = false

physics.addBody( bumper1, “static”)

bumper1.x = 300; bumper1.y = 5

local bumper2 = display.newImageRect( “marb/bumper_end.png”,1500,10 )–bumper2.isVisible = false

physics.addBody( bumper2, “static” )

bumper2.x = 200; bumper2.y = 280; bumper2.rotation = 180

local bumper3 = display.newImageRect( “marb/bumper_side.png”,10,500 )–bumper3.isVisible = false

physics.addBody( bumper3, “static”)

bumper3.x = 500; bumper3.y = 35

local bumper4 = display.newImageRect( “marb/bumper_side.png”,10,500 )–bumper4.isVisible = false

physics.addBody( bumper4, “static”)

bumper4.x = -10; bumper4.y = 35

level_menu:insert(bumper1);

level_menu:insert(bumper2);

level_menu:insert(bumper3);

level_menu:insert(bumper4);

– Override the shape declaration above, but reuse the other body properties

local ball = {}

local ballColors = {“marb2”}

local ballBody = { density=0.8, friction=0.2, bounce=0.5, radius=15 }

local n = 0

– Arrange balls in triangle formation

        n = n + 1

        ball[n] = display.newImageRect( “marb/marb2.png” ,28 ,28 )

        ball[n].x = 400

        ball[n].y = 150

        physics.addBody( ball[n], ballBody )

        ball[n].linearDamping = 0.3 – simulates friction of felt

        ball[n].angularDamping = 0.8 – stops balls from spinning forever

        ball[n].id = “ball” – store object type as string attribute

        ball[n].color = ballColors[n] – store ball color as string attribute

level_menu:insert(ball[n]);

– Create cueball

local cueball = display.newImageRect( “marb/marb.png”, 30, 30 )

cueball.x = 100

cueball.y = 150

physics.addBody( cueball, ballBody )

cueball.linearDamping = 0.3

cueball.angularDamping = 0.8

cueball.isBullet = true – force continuous collision detection, to stop really fast shots from passing through other balls

cueball.color = “white”

level_menu:insert(cueball);

–level_menu:insert(ball);

target = display.newImage( “marb/target.png” )

target.x = cueball.x; target.y = cueball.y; target.alpha = 0

local function resetCueball()

    cueball.alpha = 0

cueball.x = 100

cueball.y = 150

    cueball.xScale = 2.0; cueball.yScale = 2.0

    local dropBall = transition.to( cueball, { alpha=1.0, xScale=1.0, yScale=1.0, time=400 } )

end

– Handler for ball in pocket

local gameOver – forward declaration; function is below

local function inPocket( self, event )

    event.other:setLinearVelocity( 0, 0 )

    local fallDown = transition.to( event.other, { alpha=0, xScale=0.3, yScale=0.3, time=200 } )

    

    if ( event.other.color == “white” ) then

        timer.performWithDelay( 50, resetCueball )

    elseif ( event.other.color == “marb2” ) then

        gameOver()

    end

end

– Create pockets

local pocket = {}

–level_menu:insert(pocket[index]);

–for i = 1, 3 do

–    for j = 1, 2 do

        --local index = j + ((i-1) * 2) – a counter from 1 to 6

        – Add objects to use as collision sensors in the pockets

        local sensorRadius = 20

        pocket = display.newCircle(242, 251, 10, sensorRadius )

        pocket.x = 450

        pocket.y = 150

        – (Change this value to “true” to make the pocket sensors visible)

        pocket.isVisible = false

        

        physics.addBody( pocket, { radius=sensorRadius, isSensor=true } )

        pocket.id = “pocket”

        pocket.collision = inPocket

        pocket:addEventListener( “collision”, pocket) – add table listener to each pocket sensor

level_menu:insert(pocket);

–    end

–end

– Shoot the cue ball, using a visible force vector

local function cueShot( event )

    local t = event.target

    local phase = event.phase

    if “began” == phase then

        display.getCurrentStage():setFocus( t )

        t.isFocus = true

        

        – Stop current cueball motion, if any

        t:setLinearVelocity( 0, 0 )

        t.angularVelocity = 0

        target.x = t.x

        target.y = t.y

        

        startRotation = function()

            target.rotation = target.rotation + 4

        end

        

        Runtime:addEventListener( “enterFrame”, startRotation )

        

        local showTarget = transition.to( target, { alpha=0.4, xScale=0.4, yScale=0.4, time=200 } )

        myLine = nil

    elseif t.isFocus then

        if “moved” == phase then

            

            if ( myLine ) then

                myLine.parent:remove( myLine ) – erase previous line, if any

            end

            myLine = display.newLine( t.x,t.y, event.x,event.y )

            myLine:setColor( 255, 255, 255, 50 )

            myLine.width = 8

        elseif “ended” == phase or “cancelled” == phase then

            display.getCurrentStage():setFocus( nil )

            t.isFocus = false

            

            local stopRotation = function()

                Runtime:removeEventListener( “enterFrame”, startRotation )

            end

            

            local hideTarget = transition.to( target, { alpha=0, xScale=1.0, yScale=1.0, time=200, onComplete=stopRotation } )

            

            if ( myLine ) then

                myLine.parent:remove( myLine )

            end

            

            – Strike the ball!

            t:applyForce( (t.x - event.x), (t.y - event.y), t.x, t.y )

        end

    end

    – Stop further propagation of touch event

    return true

end

function gameOver()

    local overSplash = display.newImageRect( “marb/pass.png”, 500,370)

    overSplash.alpha = 0

    overSplash.x = 250

overSplash.y = 100

    --overSplash.xScale = 1.5; overSplash.yScale = 1.5

    local showGameOver = transition.to( overSplash, { alpha=1.0, xScale=1.0, yScale=1.0, time=500 } )

        level_menu:insert(overSplash);

    

    

    local level_menu_btn_main = display.newText(“NEXT”, 0, 0, native.systemFontBold, 16);

        level_menu_btn_main:setReferencePoint(display.CenterReferencePoint);

level_menu_btn_main.x = _w/2;

level_menu_btn_main.y = _h-10;

        level_menu_btn_main.scene = “Hselevel2”;

        level_menu:insert(level_menu_btn_main);

        level_menu_btn_main:addEventListener(“touch”, changeScene);

    

    

    

    cueball:removeEventListener( “touch”, cueShot )

    --level_menu:insert(overs);

end

– Activate the cue ball and start playing!

cueball:addEventListener( “touch”, cueShot )

        

        -----------------------------------------------------------------------------------------------------------------    

            local level_menu_btn_main1 = display.newText(“Retry”, 0, 0, native.systemFontBold, 16);

        level_menu_btn_main1:setReferencePoint(display.CenterReferencePoint);

level_menu_btn_main1.x = 450;

level_menu_btn_main1.y = _h-10;

        level_menu_btn_main1.scene = “selectlevel”;

        level_menu:insert(level_menu_btn_main1);

        level_menu_btn_main1:addEventListener(“touch”, changeScene);

        

        

        

        

        

        

        

        

    

    — insert everything into the localGroup    

    localGroup:insert(level_menu)

    

    --local cre = display.newImageRect(“credit.png”, 100,100)

    – clean everything up

    clean = function ()

    

    end

    

    – do not remove lest the sky shalt fall upon thine head

    return localGroup

    

end

------------------------------------------------------------------------------------------------------------------------------------

–*************************************************************************************

and here is the code for the second game

–lu.lua

--------------------------------------------------------------------------------------------

module(…, package.seeall)

    

function new()    

local ui = require(“ui”)

local movieclip = require(“movieclip”)

local physics = require(“physics”)

physics.start()

display.setStatusBar( display.HiddenStatusBar )

physics.setScale( 60 ) – a value that seems good for small objects (based on playtesting)

physics.setGravity( 0, 9.8 )

    level_name = “Friendly Level Name”

    level_id = “marb4” --should match lua file

    

    – we store everything inside this group at the end

    local localGroup = display.newGroup()        

    

    – change scene function

    function changeScene(e)

        if(e.phase == “ended”) then

        

            local path = system.pathForFile( “loadlast.txt”, system.DocumentsDirectory )

            – io.open opens a file at path. returns nil if no file found

            local file = io.open( path, “r” )

            if file then

               file = io.open( path, “w” )

               file:write(level_id)

               io.close( file )

            else

               – create file b/c it doesn’t exist yet

               file = io.open( path, “w” )

               file:write(level_id)

               io.close( file )

            end

        

            director:changeScene(e.target.scene, “moveFromRight”);

        end

    end

    

    local level_menu1 = display.newGroup()

        level_menu1:setReferencePoint(display.CenterReferencePoint);

        level_menu1.alpha = 0;

        

        transition.to(level_menu1, {delay=200, time=500, alpha=1});    

        

-----------------------------------------------------------------------------------------------------------------    

 – overhead view, therefore no gravity vector

–Hide status bar from the beginning

display.setStatusBar( display.HiddenStatusBar ) 

– Start Physics

    

– Set Variables

_W = display.contentWidth; – Get the width of the screen

_H = display.contentHeight; – Get the height of the screen

motionx = 0; – Variable used to move character along x axis

speed = 4; – Set Walking Speed

playerInAir = true; – Set a boolean of whether our guy is in the air or not

–************************

local gameLayer    = display.newGroup()

local bulletssLayer = display.newGroup()

local enemiesLayer = display.newGroup()

local gameIsActive = true


– Graphics

local cWidth = display.contentCenterX

local cHeight = display.contentCenterY

local gameListeners = {}

local starBoneCollision = {}

local cWidth = display.contentCenterX

local cHeight = display.contentCenterY


local gameovertxt

– Add Graphic Elements to Game

– Add Sky to the background

local sky = display.newImageRect( “jump/images/background_sky.png”, 900,250 )

    sky.x = 100

    sky.y = 120

    level_menu1:insert(sky);

    

– Add Grass floor to game

local grass_bottom = display.newImageRect( “jump/images/grass_bottom.png”, 1200,30 )

    physics.addBody( grass_bottom, “static”, { friction=0.5 } )

    grass_bottom.x = _W/2; grass_bottom.y = 260

    grass_bottom:setReferencePoint(display.BottomLeftReferencePoint);

    grass_bottom.myName = “grass”

    level_menu1:insert(grass_bottom);

guy = display.newImage( “jump/images/guy.png” )

    physics.addBody( guy, “dynamic”, {bounce= 0 })

    guy.x = 200;

    guy.y = 220;

    guy.myName = “guy”

    level_menu1:insert(guy);

    --guy.isSensor = true

guy.collision = guyCollision

guy:addEventListener(“collision”, guy)

    local circlez = display.newImageRect( “jump/images/grasss.png”,100,10)

    

     circlez.y =230

    physics.addBody( circlez,“static”, { density=3.0, friction=0.5, bounce=0.05 } )

    – remove the “isBullet” setting below to see the brick pass through cans without colliding!

 level_menu1:insert(circlez);

    

    

    function moveAngry1()

    

    

    transition.to(circlez,{time=1000, x=math.random(0,450), y=150, onComplete=moveAngry1})

    

    

end

moveAngry1()

– Add Jump button

local up = display.newImage (“jump/images/btn_arrow.png”)

    up.x = 440; up.y = 240;

    up.rotation = 270;

level_menu1:insert(up);

    

local function stop (event)

    if event.phase ==“ended” then

        motionx = 0;

    end        

end

Runtime:addEventListener(“touch”, stop )

– Make character jump

function up:touch(event)

    if(event.phase == “began” and playerInAir == false) then

        playerInAir = true

        guy:setLinearVelocity( 0, -300 )

        

    end

end

up:addEventListener(“touch”,up)

    

– Detect whether the player is in the air or not

function onCollision( event )

    if(event.object1.myName == “grass” and event.object2.myName == “guy”) then

    –    print(“collide!”)

playerInAir = false;

else 

gameOver()

end

    end

    

    function gameOver()

       print("mem "…collectgarbage(“count”))

    physics.pause()

    local overSplash1 = display.newImageRect( “marb/pass.png”, 500,370)

    overSplash1.alpha = 0

    overSplash1.x = 250

overSplash1.y = 100

    overSplash1.xScale = 1.5; overSplash1.yScale = 1.5

    local showGameOver = transition.to( overSplash1, { alpha=1.0, xScale=1.0, yScale=1.0, time=500 } )

        level_menu1:insert(overSplash1);

    

    end

    

    ----------------------

    function gameListeners(action)

    if(action == ‘add’) then

    rig:addEventListener(‘collision’, starBoneCollision)

        --bg:addEventListener(‘touch’, drawLine)

    else

        rig:removeEventListener(‘collision’, starBoneCollision)

        --bg:removeEventListener(‘touch’, drawLine)

end

end

    


–Runtime:addEventListener(“enterFrame”, gameLoop)

Runtime:addEventListener( “collision”, onCollision )

– End Game Functionality

–******************

        

        -----------------------------------------------------------------------------------------------------------------    

            local level_menu1_btn_main1 = display.newText(“Retry”, 0, 0, native.systemFontBold, 16);

        level_menu1_btn_main1:setReferencePoint(display.CenterReferencePoint);

level_menu1_btn_main1.x = 450;

level_menu1_btn_main1.y = _h-10;

        level_menu1_btn_main1.scene = “selectlevel”;

        level_menu1:insert(level_menu1_btn_main1);

        level_menu1_btn_main1:addEventListener(“touch”, changeScene);

    

        

    

    — insert everything into the localGroup    

    --localGroup:insert(btn_level_menu1)

    localGroup:insert(level_menu1)

    

    --local cre = display.newImageRect(“credit.png”, 100,100)

    – clean everything up

    clean = function ()

    

    end

    

    – do not remove lest the sky shalt fall upon thine head

    return localGroup

    

end


–****************************************

i inserted the games in the level 1 on the menu-structure-master,the first game is ok but i think there is a problem in my second game but I dont know what, my second game is like a jumping rope game , please HELP ME…