Composer.gotoScene() doesnt work using collition

So im kinda new to solar 2d and in short I made a player(which is a circle) with movement and camera using perspective.lua, some objects like lava, grass(square) and dirt. So the problem is that when i try to touch the lava with the player it does nothing, it prints that the collition is happening but when i try do go to another scene which is restart.lua it doesnt do that, and i also used print to print the variable that holds composer and it says nil
what should i do
main.lua

local composer = require("composer")
composer.gotoScene("scene1");

scene1.lua

local perspective = require("perspective")
local colorcon = require("convertcolor")

local composer = require("composer");
local scene = composer.newScene();
display.setDefault("background",colorcon.hex("87CEEB"));


print("scene1")


display.setStatusBar( display.HiddenStatusBar )


function wait(sec)
    local start = os.time()
    repeat until os.time() > start+sec
end


local pressedRight = false
local pressedLeft = false
local movementspeed = 5
local jumpSpeed = -75


local physics = require("physics");
physics.start()
physics.setGravity(0,9.81);

physics.setDrawMode("hybrid");


local materialColors = require "plugin.materialcolors"
materialColors.initHooks()

local camera = perspective.createView()

local centerX = display.contentCenterX
local centerY = display.contentCenterY



-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

-- Your code here


--circle

locationc={160,100} --x,y
local circle = display.newCircle(locationc[1],locationc[2],50);
circle:setFillColor(0,0,1,100);
physics.addBody(circle,"dynamic",
    {density = 1.0, bounce = 0.0},
    {box = {halfWidth = 30, halfHeight =5, x=0,y=53}, isSensor = true}
);
circle.isFixedRotation = true
circle.sensorOverlaps = 0

camera:add(circle,1)


--ground
locations={160,400} --x,y
local square = display.newRect(locations[1],locations[2],200,50);
square:setFillColor("#77DD77");
square.objType = "ground"
physics.addBody(square,"static",{bounce =0.0, friction = 0.3});
camera:add(square,2)

--lava
locationl ={260, 420}
local lava = display.newRect(locationl[1],locationl[2],10000,50);
lava:setFillColor("#FF0000");
lava.objType = "ground"
physics.addBody(lava,"static",{bounce =0.0, friction = 0.3});
camera:add(lava,4)


-- dirt
locationl ={260, 625}
local dirt = display.newRect(locationl[1],locationl[2], 10000,400);
dirt:setFillColor("#a4764a");
dirt.objType = "ground"
physics.addBody(dirt,"static",{bounce =0.0, friction = 0.3});
camera:add(dirt,3)


local function jump(event)
    if (event.phase =="down" and event.keyName == "up" and circle.sensorOverlaps > 0) then
        local jump = jumpSpeed
        local vx, vy = circle:getLinearVelocity()
        circle:setLinearVelocity(vx, 0)
        circle:applyLinearImpulse(nil, jump, circle.x, circle.y);
    end
end
Runtime:addEventListener("key", jump )

function keycontrol(event)
     if (event.phase =="down") then 
        pressed = true 
     elseif(event.phase == "up") then
        pressed = false
     end
end
Runtime:addEventListener("key", keycontrol)




function updatePos(event)
    if pressedRight then
        circle.x = circle.x + movementspeed
    elseif pressedLeft then 
        circle.x = circle.x - movementspeed
    end
end
function keyListener(event)
    local key = event.keyName
    local phase = event.phase
    if phase =="down" then
        if key =="right" then
            pressedRight = true
        elseif key == "left" then
            pressedLeft = true
        end
    elseif phase == "up" then
        if key =="right" then
            pressedRight = false
        elseif key == "left" then
            pressedLeft = false
        end
    end 
end           
Runtime:addEventListener("key", keyListener)

function FrameListener()
    updatePos()
    
end
Runtime:addEventListener("enterFrame", FrameListener)



local function sensorCollide(self, event)
    if(event.selfElement ==2 and event.other.objType =="ground") then
      if(event.phase == "began") then
          self.sensorOverlaps = self.sensorOverlaps +1
      elseif(event.phase == "ended") then
          self.sensorOverlaps = self.sensorOverlaps-1
      end
  end
end

circle.collision = sensorCollide
circle:addEventListener("collision") 



local function lavacollide(self,event)
    if(event.phase == "began") then
        local restart = composer.gotoScene("restart");
        print(restart)
        print("collition")
    else
    return true 
    end
end

lava.collision = lavacollide
lava:addEventListener( "collision" )



camera:setFocus(circle)
camera:track()




return scene

restart.lua

local composer = require("composer")
local scene = composer.newScene();
 
print("restart")


return scene

Welcome!

You are probably facing this problem because your restart.lua code is missing the needed functions for composer. Please take a look at this segment in the docs:

Look, I read the documentation and don’t really understand what to do, I tried to add a circle in restart.lua, but it loads the circle in the same scene. So my idea was to go from scene1.lua to restart.lua and everything from scene 1 disappears but when I press a button in restart.lua it restarts the code in scene1. I read that there isn’t such thing as restarting the code but you can do something like this by using composer. I really don’t know what to do now, but if I get some help and maybe an instruction on what I was doing wrong and what I should do, I would really appreciate it!

That’s the reason I linked to a specific part of the documentation. You need to use the scene template to make a file into a scene in composer. Your restart.lua is not a scene in the first sample code you provided. You need scene:create(), scene:show() etc. to make it into a scene. After that, you can switch between scenes.

look I tried but still no good, can you tell me what did I do wrong:
scene1.lua

local perspective = require("perspective")
local colorcon = require("convertcolor")

local composer = require("composer");
local scene = composer.newScene();
display.setDefault("background",colorcon.hex("87CEEB"));


function scene:create(event)
      local scenegr = self.view
end


display.setStatusBar( display.HiddenStatusBar )


function wait(sec)
    local start = os.time()
    repeat until os.time() > start+sec
end


local pressedRight = false
local pressedLeft = false
local movementspeed = 5
local jumpSpeed = -75


local physics = require("physics");
physics.start()
physics.setGravity(0,9.81);

physics.setDrawMode("hybrid");


local materialColors = require "plugin.materialcolors"
materialColors.initHooks()

local camera = perspective.createView()

local centerX = display.contentCenterX
local centerY = display.contentCenterY



-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

-- Your code here


--circle
function scene:show(event)
        local scenegr = self.view
        local phase = event.phase

        if(phase == "will") then
        locationc={160,100} --x,y
        circle = display.newCircle(locationc[1],locationc[2],50);
        circle:setFillColor(0,0,1,100);
        physics.addBody(circle,"dynamic",
        {density = 1.0, bounce = 0.0},
        {box = {halfWidth = 30, halfHeight =5, x=0,y=53}, isSensor = true}
        );
        circle.isFixedRotation = true
        circle.sensorOverlaps = 0




        --ground
        locations={160,400} --x,y
        square = display.newRect(locations[1],locations[2],200,50);
        square:setFillColor("#77DD77");
        square.objType = "ground"
        physics.addBody(square,"static",{bounce =0.0, friction = 0.3});


        -- dirt
        locationl ={260, 625}
        dirt = display.newRect(locationl[1],locationl[2], 10000,400);
        dirt:setFillColor("#a4764a");
        dirt.objType = "ground"
        physics.addBody(dirt,"static",{bounce =0.0, friction = 0.3});


        --lava
        locationl ={260, 420}
        lava = display.newRect(locationl[1],locationl[2],10000,50);
        lava:setFillColor("#FF0000");
        lava.objType = "ground"
        physics.addBody(lava,"static",{bounce =0.0, friction = 0.3});






        local function jump(event)
            if (event.phase =="down" and event.keyName == "up" and circle.sensorOverlaps > 0) then
                local jump = jumpSpeed
                local vx, vy = circle:getLinearVelocity()
                circle:setLinearVelocity(vx, 0)
                circle:applyLinearImpulse(nil, jump, circle.x, circle.y);
            end
        end
        Runtime:addEventListener("key", jump )

        function keycontrol(event)
            if (event.phase =="down") then 
                pressed = true 
            elseif(event.phase == "up") then
                pressed = false
            end
        end
        Runtime:addEventListener("key", keycontrol)




        function updatePos(event)
            if pressedRight then
                circle.x = circle.x + movementspeed
            elseif pressedLeft then 
                circle.x = circle.x - movementspeed
            end
        end

        function keyListener(event)
            local key = event.keyName
            local phase = event.phase
            if phase =="down" then
                if key =="right" then
                    pressedRight = true
                elseif key == "left" then
                    pressedLeft = true
                end
            elseif phase == "up" then
                if key =="right" then
                    pressedRight = false
                elseif key == "left" then
                    pressedLeft = false
                end
            end 
        end 

        Runtime:addEventListener("key", keyListener)

        function FrameListener()
            updatePos()
            
        end

        Runtime:addEventListener("enterFrame", FrameListener)

        --local bacteria = display.newImage("IMG_4647.jpg",-600,300);
        --camera:add(bacteria, 5)



        local function sensorCollide(self, event)
            if(event.selfElement ==2 and event.other.objType =="ground") then
            if(event.phase == "began") then
                self.sensorOverlaps = self.sensorOverlaps +1
            elseif(event.phase == "ended") then
                self.sensorOverlaps = self.sensorOverlaps-1
            end
        end
        end

        circle.collision = sensorCollide
        circle:addEventListener("collision") 



        local function lavacollide(self,event)
            if(event.phase == "began") then
                composer.removeScene("scene1");
                composer.gotoScene("restart");
            
                print("collition")
            else
            return true 
            end
        end

        lava.collision = lavacollide
        lava:addEventListener( "collision" )

        camera:add(circle,1)
        camera:add(square,2)
        camera:add(dirt,3)  
        camera:add(lava,4)

        camera:setFocus(circle)
        camera:track()

    elseif(phase == "did") then
    end
end




scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )

return scene

restart.lua

local composer = require("composer")
local scene = composer.newScene();

function scene:destroy(event)

end

function scene:create(event)
    local sceneGroup = self.view
    print("restart created")
end



function scene:show( event )
    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then
        print("restart will appear")
    elseif ( phase == "did" ) then
        print("restart appeared")
        local circle = display.newCircle(160,200,40);
    end
end


scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
return scene

I can’t delete/destroy scene1, what code should I use?

If destroy function is not being called that’s probably because you forgot to add “destroy” listener:

scene:addEventListener( "destroy", scene )

Your “composer” scenes should be structured as shown in the scene template that I linked earlier. Please copy/paste the template and place your code pieces in corresponding areas.

I’m sorry, I don’t have time to put together a sample project for you to show how Composer library works. I believe there is a sample in the simulator. You can also check out my game code to examine how it works. Simply looking at logoScreen.luamainMenu.lua should do the trick.

I’ve got a number of composer examples here: https://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/composer_scene_manager.zip

You might want to look at example 10 (among others).

Index of examples: