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