Hey guys so I need help switching scenes I got all the code in scene one and it works but when I try to switch to scene 2 on a certain event I get and error I will post my code below. (I took the code from the composer example)
I don’t know if I am doing it right or not? The error is (error loading module ‘scene2’ from scene2.lua)
Any help thanks!
Scene1.lua
_local sceneName = …
local composer = require( “composer” )
– Load scene with same root filename as this file
local scene = composer.newScene( sceneName )
function scene:create( event )
local sceneGroup = self.view
local physics = require(“physics”)
physics.start()
– Called when the scene’s view does not exist
–
– INSERT code here to initialize the scene
– e.g. add display objects to ‘sceneGroup’, add touch listeners, etc
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == “will” then
– Called when the scene is still off screen and is about to move on screen
elseif phase == “did” then
– Called when the scene is now on screen
–
– INSERT code here to make the scene come alive
– e.g. start timers, begin animation, play audio, etc
– we obtain the object by id from the scene’s object hierarchy
local score = require( “score” )
local scoreText = score.init({
fontSize = 20,
font = “Helvetica”,
x = 25,
y = 00,
maxDigits = 4,
leadingZeros = true,
filename = “score.lua”,
})
scoreText:setFillColor(1,0,0)
local numberSmiles = 1 --local variable; amount can be changed
local function spawnSmiles()
for i=1,numberSmiles do
local smile = display.newImageRect(“parachuteguy.png”, 45, 45);
– smile:setReferencePoint(display.CenterReferencePoint); --not necessary; center is default
smile.x = math.random(-10, 400);
smile.y = -40;
transition.to( smile, { time=math.random(2000,8000), x=math.random(-10,400) , y=600, onComplete=clearSmile } );
physics.addBody( smile, “dynamic”, { density=0.1, bounce=0.1, friction=0.1, radius=0 } );
—scale the parachute actor --Adding touch event
smile:scale(3,5)
local function onObjectTouch( event )
if event.phase == “began” then
print( "Touch event began on: " )
smile:removeSelf()
score.add(event.target.value)
print(math.random(10))
end
return true
end
smile:addEventListener( “touch”, onObjectTouch )
end
end
timer.performWithDelay( 1000,spawnSmiles, 0 ) --fire every second
local timeLimit = 20
timeLeft = display.newText(timeLimit, 160, 20, native.systemFontBold, 14)
timeLeft:setTextColor(255,0,0)
local function timerDown()
timeLimit = timeLimit-1
timeLeft.text = timeLimit
if(timeLimit==0)then
composer.gotoScene(“scene2”)
print(“Time Out”) – or do your code for time out
end
end
timer.performWithDelay(1000,timerDown,timeLimit)
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if event.phase == “will” then
– Called when the scene is on screen and is about to move off screen
–
– INSERT code here to pause the scene
– e.g. stop timers, stop animation, unload sounds, etc.)
elseif phase == “did” then
– Called when the scene is now off screen
end
end
function scene:destroy( event )
local sceneGroup = self.view
– Called prior to the removal of scene’s “view” (sceneGroup)
–
– INSERT code here to cleanup the scene
– e.g. remove display objects, remove touch listeners, save state, etc
end
– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene_
scene2.lua
_local sceneName = scene2
local composer = require( “composer” )
– Load scene with same root filename as this file
local scenecomposer.newScene( scene2 )
function scene:create( event )
local sceneGroup = self.view
– Called when the scene’s view does not exist
–
– INSERT code here to initialize the scene
– e.g. add display objects to ‘sceneGroup’, add touch listeners, etc
end
function scene:show( event )
local sceneGroup = self.view
local testText = display.newText(“This is Scene2”, 160, 20, native.systemFontBold, 14)
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if event.phase == “will” then
– Called when the scene is on screen and is about to move off screen
–
– INSERT code here to pause the scene
– e.g. stop timers, stop animation, unload sounds, etc.)
elseif phase == “did” then
– Called when the scene is now off screen
end
end
function scene:destroy( event )
local sceneGroup = self.view
– Called prior to the removal of scene’s “view” (sceneGroup)
–
– INSERT code here to cleanup the scene
– e.g. remove display objects, remove touch listeners, save state, etc
end
– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene_