Composer not working but also not giving me any errors.

I have a small game I created, initially I created the game as the main.lua file and everything was going great, however I would like to make a game over screen and a winner screen. So I looked into composer to do this, but I am having a hard time even loading anything on screen.

I dont get any errors in the corona terminal but I also don’t get anything in the corona simulator screen. Not sure what I am doing wrong. 

Here is the code, if anyone can help me with this I would really appreciate it.

local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- -- local forward references should go here -- ------------------------------------------------------------------------------- -- "scene:create()" function scene:create( event ) local sceneGroup = self.view -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. --Image Assets stoneGroup = display.newGroup() local bg = display.newImageRect("images/GameBackground.png",display.viewableContentHeight\*2,display.viewableContentWidth\*2) bg.x= display.viewableContentWidth/2 bg.y = display.viewableContentHeight/2 bg.anchorX = 0.5 bg.anchorY = 0.5 physics = require("physics") physics.start() physics.setDrawMode( "normal" ) sceneGroup:insert(bg) local bg2 = display.newImageRect("images/GameBackground.png",display.viewableContentHeight\*2,display.viewableContentWidth\*2) bg2.x= display.viewableContentWidth\*4 bg2.y = display.viewableContentHeight/2 bg2.anchorX = 0.5 bg2.anchorY = 0.5 sceneGroup:insert(bg2) local ground = display.newRect(display.viewableContentWidth/2,display.viewableContentHeight-84,display.viewableContentWidth\*3.5, 65) ground.anchorX = 0.5 ground.anchorY = 0.5 ground:setFillColor(0,0,0) physics.addBody( ground, "static", { density=3.0, friction=0.5, bounce=0.1} ) sceneGroup:insert(ground) local ghost = display.newImageRect("images/Ghost.png", 83,55) ghost.y = display.viewableContentHeight-170 physics.addBody( ghost, "dynamic", { density=3, friction=5.5, bounce=.3 } ) ghost.isFixedRotation = true sceneGroup:insert(ghost) local stoned = {"images/Bones\_Gravestone\_nograss.png", "images/Skull\_Gravestone\_nograss.png", "images/Web\_Gravestone\_nograss.png"} --Stones local stone1 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone1.x = display.viewableContentWidth \* 2.2 stone1.anchorX = 0.5 stone1.anchorY = 0.5 stone1.y = (ground.y - stone1.height-3) sceneGroup:insert(stone1) local stone2 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone2.x = display.viewableContentWidth \* 3.2 stone2.anchorX = 0.5 stone2.anchorY = 0.5 stone2.y = (ground.y - stone2.height-3) sceneGroup:insert(stone2) local stone3 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone3.x = display.viewableContentWidth \* 5.2 stone3.anchorX = 0.5 stone3.anchorY = 0.5 stone3.y = (ground.y - stone2.height-3) sceneGroup:insert(stone3) local stone4 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone4.x = display.viewableContentWidth \* 7.2 stone4.anchorX = 0.5 stone4.anchorY = 0.5 stone4.y = (ground.y - stone2.height-3) sceneGroup:insert(stone4) local stone5 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone5.x = display.viewableContentWidth \* 8.5 stone5.anchorX = 0.5 stone5.anchorY = 0.5 stone5.y = (ground.y - stone2.height-3) sceneGroup:insert(stone5) --Grass local grass = display.newImageRect("images/Grass.png", display.viewableContentWidth\*5, 200) grass.anchorX = .5 grass.y = (display.viewableContentHeight - 171) sceneGroup:insert(grass) --Wall Left local leftWall = display.newRect(display.viewableContentWidth-display.viewableContentWidth\*2.13, 200,100,display.viewableContentHeight) physics.addBody(leftWall,"kinematic", { density=10, friction=10, bounce=5 } ) leftWall.alpha=0 sceneGroup:insert(leftWall) local rightWall = display.newRect(display.viewableContentWidth\*2.13, 200,100,display.viewableContentHeight) physics.addBody(rightWall,"kinematic", { density=10, friction=10, bounce=5 } ) rightWall.alpha=0 sceneGroup:insert(rightWall) --Coin local coin = display.newImageRect("images/Coin.png", 45, 45) coin.x = display.viewableContentWidth\*2.5 coin.y = 350 physics.addBody( coin, "kinematic", { density=0, friction=0, bounce=0 } ) sceneGroup:insert(coin) local coin2 = display.newImageRect("images/Coin.png", 45, 45) coin2.x = coin.x\*2.5 coin2.y = 350 physics.addBody( coin2, "kinematic", { density=0, friction=0, bounce=0 } ) sceneGroup:insert(coin2) --Score scoreNum = tonumber(0) local score = display.newText(scoreNum, 0, 0, native.systemFont, 32) score.anchorX = .5 score.anchorY = .5 score.x = display.viewableContentWidth\*1.92 score.y = 50 sceneGroup:insert(score) --scoreIcon local scoreIcon = display.newImageRect("images/Coin.png", 45, 45) scoreIcon.y = score.y-2 scoreIcon.x = score.x - 50 sceneGroup:insert(scoreIcon) -- Listener Functions --Scrolls bg function scrollBG(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*4 else self.x = self.x - 3 end end function scrollBG2(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*4 else self.x = self.x - 3 end end -- Jump Function function jump(event) if event.phase == "began" and ghost.y \>= (display.viewableContentHeight-200) then ghost:applyForce(200, -4500, ghost.x, ghost.y) end return true end --Scroll Tomb Stone function scrollStone(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*math.random(2,5) else self.x = self.x - 5 end end --Scroll Coin function scrollCoin(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*math.random(2,5) self.y = math.random(300, 400) physics.addBody(coin, "kinematic", { density=0, friction=0, bounce=0 }) transition.to(coin, {time=300, alpha=100, width=45, height=45}) else self.x = self.x - 5 end end --Scroll Coin function scrollCoin2(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*math.random(2,5) self.y = math.random(300, 400) physics.addBody(coin2,"kinematic", { density=0, friction=0, bounce=0 }) transition.to(coin2, {time=300, alpha=100, width=45, height=45}) else self.x = self.x - 5 end end local function removeTheBody() physics.removeBody(coin) end local function removeTheBody2() physics.removeBody(coin2) end --Coin Collision local function onCoinCollision( self, event ) transition.to(coin, {time=300, alpha=0, width=10, height=30}) scoreNum = scoreNum+1 score.text = scoreNum timer.performWithDelay( 1, removeTheBody ) end local function onCoin2Collision( self, event ) transition.to(coin2, {time=300, alpha=0, width=10, height=30}) scoreNum = scoreNum+1 score.text = scoreNum timer.performWithDelay( 1, removeTheBody2 ) end --Stone Physics physics.addBody( stone1, "static", { density=3, friction=5.5, bounce=.3 } ) physics.addBody( stone2, "static", { density=3, friction=5.5, bounce=.3 } ) physics.addBody( stone3, "static", { density=3, friction=5.5, bounce=.3 } ) physics.addBody( stone4, "static", { density=3, friction=5.5, bounce=.3 } ) physics.addBody( stone5, "static", { density=3, friction=5.5, bounce=.3 } ) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then --Tomb Stoned --Event Listeners bg.enterFrame=scrollBG Runtime:addEventListener("enterFrame", bg) bg2.enterFrame=scrollBG2 Runtime:addEventListener("enterFrame", bg2) stone1.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone1) stone2.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone2) stone3.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone3) stone4.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone4) stone5.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone5) --Ghost Listener Runtime:addEventListener("touch", jump) --Coin Move coin.enterFrame=scrollCoin Runtime:addEventListener("enterFrame", coin) coin.collision=onCoinCollision coin:addEventListener("collision", coin) --Coin Move coin2.enterFrame=scrollCoin2 Runtime:addEventListener("enterFrame", coin2) coin2.collision=onCoin2Collision coin2:addEventListener("collision", coin2) -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view ("sceneGroup"). -- Insert code here to clean up the scene. -- Example: remove display objects, save state, etc. end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

Hi @sebastianflores,

Chances are very high that this is a simple scope issue. Remember that all of the Composer scene functions (:create(), :show(), etc.) have their own scope, as with all Lua code blocks. So if you put some function (like a scrolling thing) inside the :create() function, and then you try to call it from the :show() function, Lua will have no idea what you’re talking about because the scope is done incorrectly.

The same goes for objects. If you create “stone” as a local object within :create(), but then you try to reference it in :show(), Lua does not know what object you are talking about.

I suggest you go through this carefully and fix all of the scope issues. That should fix things.

Best regards,

Brent

I tried just creating a rectangle on screen but I am still getting nothing

Here is the code I wrote to do that. 

local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- -- local forward references should go here -- ------------------------------------------------------------------------------- -- "scene:create()" function scene:create( event ) local sceneGroup = self.view -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. rext = display.newRect(200,200,200,200) rext:setFillColor(.3,.3,.2) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view ("sceneGroup"). -- Insert code here to clean up the scene. -- Example: remove display objects, save state, etc. end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

Hi @sebastianflores,

One very critical aspect is that you need to add Composer scene objects to the scene’s view. That way, they will be “managed” as part of the scene.

If this is your first time using Composer, you should absolutely read some tutorials, guides, watch videos, etc. There are some fundamental things you must understand before just diving into Composer unprepared.

GUIDE:

https://docs.coronalabs.com/guide/system/composer/index.html

TUTORIAL/VIDEO ROUNDUP:

https://coronalabs.com/blog/2015/08/11/tutorial-treasury-composer/

Hi thank you for those sources, I checked them out and I was able to get image loading and physics started and applied to all my objects.

However for some reason my functions and listeners are not working,

I keep getting the following error game.lua.264:attempt to index global bg a nil value

stack traceback:game.lua:264: in function <game.lua:154>

in function<?:865>

(tail call):?

in functions <?:469>

in functions <?:221>

With the following code: 

----------------------------------------------------------------------------------------- -- Level -- main.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- -- local forward references should go here -- ------------------------------------------------------------------------------- -- "scene:create()" function scene:create( event ) local sceneGroup = self.view -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. --Image Assets local bg = display.newImageRect("images/GameBackground.png",display.viewableContentHeight\*2,display.viewableContentWidth\*2) bg.x= display.viewableContentWidth/2 bg.y = display.viewableContentHeight/2 bg.anchorX = 0.5 bg.anchorY = 0.5 physics = require("physics") physics.start() physics.setDrawMode( "hybrid" ) sceneGroup:insert(bg) local bg2 = display.newImageRect("images/GameBackground.png",display.viewableContentHeight\*2,display.viewableContentWidth\*2) bg2.x= display.viewableContentWidth\*4 bg2.y = display.viewableContentHeight/2 bg2.anchorX = 0.5 bg2.anchorY = 0.5 sceneGroup:insert(bg2) local ground = display.newRect(display.viewableContentWidth/2,display.viewableContentHeight-84,display.viewableContentWidth\*3.5, 65) ground.anchorX = 0.5 ground.anchorY = 0.5 ground:setFillColor(0,0,0) physics.addBody( ground, "static", { density=3.0, friction=0.5, bounce=0.1} ) sceneGroup:insert(ground) local ghost = display.newImageRect("images/Ghost.png", 83,55) ghost.y = display.viewableContentHeight-170 physics.addBody( ghost, "dynamic", { density=3, friction=5.5, bounce=.3 } ) ghost.isFixedRotation = true local stoned = {"images/Bones\_Gravestone\_nograss.png", "images/Skull\_Gravestone\_nograss.png", "images/Web\_Gravestone\_nograss.png"} --Stones local stone1 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone1.x = display.viewableContentWidth \* 2.2 stone1.anchorX = 0.5 stone1.anchorY = 0.5 stone1.y = (ground.y - stone1.height-3) sceneGroup:insert(stone1) local stone2 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone2.x = display.viewableContentWidth \* 3.2 stone2.anchorX = 0.5 stone2.anchorY = 0.5 stone2.y = (ground.y - stone2.height-3) sceneGroup:insert(stone2) local stone3 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone3.x = display.viewableContentWidth \* 5.2 stone3.anchorX = 0.5 stone3.anchorY = 0.5 stone3.y = (ground.y - stone2.height-3) sceneGroup:insert(stone3) local stone4 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone4.x = display.viewableContentWidth \* 7.2 stone4.anchorX = 0.5 stone4.anchorY = 0.5 stone4.y = (ground.y - stone2.height-3) sceneGroup:insert(stone4) local stone5 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone5.x = display.viewableContentWidth \* 8.5 stone5.anchorX = 0.5 stone5.anchorY = 0.5 stone5.y = (ground.y - stone2.height-3) sceneGroup:insert(stone5) --Grass local grass = display.newImageRect("images/Grass.png", display.viewableContentWidth\*5, 200) grass.anchorX = .5 grass.y = (display.viewableContentHeight - 171) sceneGroup:insert(grass) --Wall Left local leftWall = display.newRect(display.viewableContentWidth-display.viewableContentWidth\*2.13, 200,100,display.viewableContentHeight) physics.addBody(leftWall,"kinematic", { density=10, friction=10, bounce=5 } ) leftWall.alpha=0 sceneGroup:insert(leftWall) local rightWall = display.newRect(display.viewableContentWidth\*2.13, 200,100,display.viewableContentHeight) physics.addBody(rightWall,"kinematic", { density=10, friction=10, bounce=5 } ) rightWall.alpha=0 sceneGroup:insert(rightWall) --Coin local coin = display.newImageRect("images/Coin.png", 45, 45) coin.x = display.viewableContentWidth\*2.5 coin.y = 350 physics.addBody( coin, "kinematic", { density=0, friction=0, bounce=0 } ) sceneGroup:insert(coin) local coin2 = display.newImageRect("images/Coin.png", 45, 45) coin2.x = coin.x\*2.5 coin2.y = 350 physics.addBody( coin2, "kinematic", { density=0, friction=0, bounce=0 } ) sceneGroup:insert(coin2) --Score scoreNum = tonumber(0) local score = display.newText(scoreNum, 0, 0, native.systemFont, 32) score.anchorX = .5 score.anchorY = .5 score.x = display.viewableContentWidth\*1.92 score.y = 50 sceneGroup:insert(score) --scoreIcon local scoreIcon = display.newImageRect("images/Coin.png", 45, 45) scoreIcon.y = score.y-2 scoreIcon.x = score.x - 50 sceneGroup:insert(scoreIcon) --Stone Physics physics.addBody( stone1, "static", { density=3, friction=5.5, bounce=.3 } ) physics.addBody( stone2, "static", { density=3, friction=5.5, bounce=.3 } ) physics.addBody( stone3, "static", { density=3, friction=5.5, bounce=.3 } ) physics.addBody( stone4, "static", { density=3, friction=5.5, bounce=.3 } ) physics.addBody( stone5, "static", { density=3, friction=5.5, bounce=.3 } ) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). --Start -- Listener Functions --Scrolls bg function scrollBG(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*4 else self.x = self.x - 3 end end function scrollBG2(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*4 else self.x = self.x - 3 end end -- Jump Function function jump(event) if event.phase == "began" and ghost.y \>= (display.viewableContentHeight-200) then ghost:applyForce(200, -4500, ghost.x, ghost.y) end return true end --Scroll Tomb Stone function scrollStone(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*math.random(2,5) else self.x = self.x - 5 end end --Scroll Coin function scrollCoin(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*math.random(2,5) self.y = math.random(300, 400) physics.addBody(coin, "kinematic", { density=0, friction=0, bounce=0 }) transition.to(coin, {time=300, alpha=100, width=45, height=45}) else self.x = self.x - 5 end end --Scroll Coin function scrollCoin2(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*math.random(2,5) self.y = math.random(300, 400) physics.addBody(coin2,"kinematic", { density=0, friction=0, bounce=0 }) transition.to(coin2, {time=300, alpha=100, width=45, height=45}) else self.x = self.x - 5 end end local function removeTheBody() physics.removeBody(coin) end local function removeTheBody2() physics.removeBody(coin2) end --Coin Collision local function onCoinCollision( self, event ) transition.to(coin, {time=300, alpha=0, width=10, height=30}) scoreNum = scoreNum+1 score.text = scoreNum timer.performWithDelay( 1, removeTheBody ) end local function onCoin2Collision( self, event ) transition.to(coin2, {time=300, alpha=0, width=10, height=30}) scoreNum = scoreNum+1 score.text = scoreNum timer.performWithDelay( 1, removeTheBody2 ) end --End elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. --Start --Tomb Stoned --Event Listeners bg.enterFrame=scrollBG Runtime:addEventListener("enterFrame", bg) bg2.enterFrame=scrollBG2 Runtime:addEventListener("enterFrame", bg2) stone1.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone1) stone2.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone2) stone3.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone3) stone4.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone4) stone5.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone5) --Ghost Listener Runtime:addEventListener("touch", jump) --Coin Move coin.enterFrame=scrollCoin Runtime:addEventListener("enterFrame", coin) coin.collision=onCoinCollision coin:addEventListener("collision", coin) --Coin Move coin2.enterFrame=scrollCoin2 Runtime:addEventListener("enterFrame", coin2) coin2.collision=onCoin2Collision coin2:addEventListener("collision", coin2) --End end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view ("sceneGroup"). -- Insert code here to clean up the scene. -- Example: remove display objects, save state, etc. end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

Hi again,

This is a scope issue, which I mentioned in my first post. You’ve created “bg” as a local variable inside the :create() function, but then you try to point to it outside that function. Lua does not know what you’re referring to, since it’s out of scope, and thus it throws the error.

Best regards,

Brent

Hi thank you for the help, I really appreciate it.

I have everything almost working correctly. I have a try again menu that pops up if a user touches a rock. The user has the option to try again but when I load the game scene so they can try again, the scene is loaded with them touching the rock again right where they left off. This make them immediately go back to the try again menu. Is there a way to go to a scene but have the script start over when the scene is loaded?

Hi @sebastianflores,

Chances are very high that this is a simple scope issue. Remember that all of the Composer scene functions (:create(), :show(), etc.) have their own scope, as with all Lua code blocks. So if you put some function (like a scrolling thing) inside the :create() function, and then you try to call it from the :show() function, Lua will have no idea what you’re talking about because the scope is done incorrectly.

The same goes for objects. If you create “stone” as a local object within :create(), but then you try to reference it in :show(), Lua does not know what object you are talking about.

I suggest you go through this carefully and fix all of the scope issues. That should fix things.

Best regards,

Brent

I tried just creating a rectangle on screen but I am still getting nothing

Here is the code I wrote to do that. 

local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- -- local forward references should go here -- ------------------------------------------------------------------------------- -- "scene:create()" function scene:create( event ) local sceneGroup = self.view -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. rext = display.newRect(200,200,200,200) rext:setFillColor(.3,.3,.2) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view ("sceneGroup"). -- Insert code here to clean up the scene. -- Example: remove display objects, save state, etc. end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

Hi @sebastianflores,

One very critical aspect is that you need to add Composer scene objects to the scene’s view. That way, they will be “managed” as part of the scene.

If this is your first time using Composer, you should absolutely read some tutorials, guides, watch videos, etc. There are some fundamental things you must understand before just diving into Composer unprepared.

GUIDE:

https://docs.coronalabs.com/guide/system/composer/index.html

TUTORIAL/VIDEO ROUNDUP:

https://coronalabs.com/blog/2015/08/11/tutorial-treasury-composer/

Hi thank you for those sources, I checked them out and I was able to get image loading and physics started and applied to all my objects.

However for some reason my functions and listeners are not working,

I keep getting the following error game.lua.264:attempt to index global bg a nil value

stack traceback:game.lua:264: in function <game.lua:154>

in function<?:865>

(tail call):?

in functions <?:469>

in functions <?:221>

With the following code: 

----------------------------------------------------------------------------------------- -- Level -- main.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- -- local forward references should go here -- ------------------------------------------------------------------------------- -- "scene:create()" function scene:create( event ) local sceneGroup = self.view -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. --Image Assets local bg = display.newImageRect("images/GameBackground.png",display.viewableContentHeight\*2,display.viewableContentWidth\*2) bg.x= display.viewableContentWidth/2 bg.y = display.viewableContentHeight/2 bg.anchorX = 0.5 bg.anchorY = 0.5 physics = require("physics") physics.start() physics.setDrawMode( "hybrid" ) sceneGroup:insert(bg) local bg2 = display.newImageRect("images/GameBackground.png",display.viewableContentHeight\*2,display.viewableContentWidth\*2) bg2.x= display.viewableContentWidth\*4 bg2.y = display.viewableContentHeight/2 bg2.anchorX = 0.5 bg2.anchorY = 0.5 sceneGroup:insert(bg2) local ground = display.newRect(display.viewableContentWidth/2,display.viewableContentHeight-84,display.viewableContentWidth\*3.5, 65) ground.anchorX = 0.5 ground.anchorY = 0.5 ground:setFillColor(0,0,0) physics.addBody( ground, "static", { density=3.0, friction=0.5, bounce=0.1} ) sceneGroup:insert(ground) local ghost = display.newImageRect("images/Ghost.png", 83,55) ghost.y = display.viewableContentHeight-170 physics.addBody( ghost, "dynamic", { density=3, friction=5.5, bounce=.3 } ) ghost.isFixedRotation = true local stoned = {"images/Bones\_Gravestone\_nograss.png", "images/Skull\_Gravestone\_nograss.png", "images/Web\_Gravestone\_nograss.png"} --Stones local stone1 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone1.x = display.viewableContentWidth \* 2.2 stone1.anchorX = 0.5 stone1.anchorY = 0.5 stone1.y = (ground.y - stone1.height-3) sceneGroup:insert(stone1) local stone2 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone2.x = display.viewableContentWidth \* 3.2 stone2.anchorX = 0.5 stone2.anchorY = 0.5 stone2.y = (ground.y - stone2.height-3) sceneGroup:insert(stone2) local stone3 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone3.x = display.viewableContentWidth \* 5.2 stone3.anchorX = 0.5 stone3.anchorY = 0.5 stone3.y = (ground.y - stone2.height-3) sceneGroup:insert(stone3) local stone4 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone4.x = display.viewableContentWidth \* 7.2 stone4.anchorX = 0.5 stone4.anchorY = 0.5 stone4.y = (ground.y - stone2.height-3) sceneGroup:insert(stone4) local stone5 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone5.x = display.viewableContentWidth \* 8.5 stone5.anchorX = 0.5 stone5.anchorY = 0.5 stone5.y = (ground.y - stone2.height-3) sceneGroup:insert(stone5) --Grass local grass = display.newImageRect("images/Grass.png", display.viewableContentWidth\*5, 200) grass.anchorX = .5 grass.y = (display.viewableContentHeight - 171) sceneGroup:insert(grass) --Wall Left local leftWall = display.newRect(display.viewableContentWidth-display.viewableContentWidth\*2.13, 200,100,display.viewableContentHeight) physics.addBody(leftWall,"kinematic", { density=10, friction=10, bounce=5 } ) leftWall.alpha=0 sceneGroup:insert(leftWall) local rightWall = display.newRect(display.viewableContentWidth\*2.13, 200,100,display.viewableContentHeight) physics.addBody(rightWall,"kinematic", { density=10, friction=10, bounce=5 } ) rightWall.alpha=0 sceneGroup:insert(rightWall) --Coin local coin = display.newImageRect("images/Coin.png", 45, 45) coin.x = display.viewableContentWidth\*2.5 coin.y = 350 physics.addBody( coin, "kinematic", { density=0, friction=0, bounce=0 } ) sceneGroup:insert(coin) local coin2 = display.newImageRect("images/Coin.png", 45, 45) coin2.x = coin.x\*2.5 coin2.y = 350 physics.addBody( coin2, "kinematic", { density=0, friction=0, bounce=0 } ) sceneGroup:insert(coin2) --Score scoreNum = tonumber(0) local score = display.newText(scoreNum, 0, 0, native.systemFont, 32) score.anchorX = .5 score.anchorY = .5 score.x = display.viewableContentWidth\*1.92 score.y = 50 sceneGroup:insert(score) --scoreIcon local scoreIcon = display.newImageRect("images/Coin.png", 45, 45) scoreIcon.y = score.y-2 scoreIcon.x = score.x - 50 sceneGroup:insert(scoreIcon) --Stone Physics physics.addBody( stone1, "static", { density=3, friction=5.5, bounce=.3 } ) physics.addBody( stone2, "static", { density=3, friction=5.5, bounce=.3 } ) physics.addBody( stone3, "static", { density=3, friction=5.5, bounce=.3 } ) physics.addBody( stone4, "static", { density=3, friction=5.5, bounce=.3 } ) physics.addBody( stone5, "static", { density=3, friction=5.5, bounce=.3 } ) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). --Start -- Listener Functions --Scrolls bg function scrollBG(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*4 else self.x = self.x - 3 end end function scrollBG2(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*4 else self.x = self.x - 3 end end -- Jump Function function jump(event) if event.phase == "began" and ghost.y \>= (display.viewableContentHeight-200) then ghost:applyForce(200, -4500, ghost.x, ghost.y) end return true end --Scroll Tomb Stone function scrollStone(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*math.random(2,5) else self.x = self.x - 5 end end --Scroll Coin function scrollCoin(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*math.random(2,5) self.y = math.random(300, 400) physics.addBody(coin, "kinematic", { density=0, friction=0, bounce=0 }) transition.to(coin, {time=300, alpha=100, width=45, height=45}) else self.x = self.x - 5 end end --Scroll Coin function scrollCoin2(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*math.random(2,5) self.y = math.random(300, 400) physics.addBody(coin2,"kinematic", { density=0, friction=0, bounce=0 }) transition.to(coin2, {time=300, alpha=100, width=45, height=45}) else self.x = self.x - 5 end end local function removeTheBody() physics.removeBody(coin) end local function removeTheBody2() physics.removeBody(coin2) end --Coin Collision local function onCoinCollision( self, event ) transition.to(coin, {time=300, alpha=0, width=10, height=30}) scoreNum = scoreNum+1 score.text = scoreNum timer.performWithDelay( 1, removeTheBody ) end local function onCoin2Collision( self, event ) transition.to(coin2, {time=300, alpha=0, width=10, height=30}) scoreNum = scoreNum+1 score.text = scoreNum timer.performWithDelay( 1, removeTheBody2 ) end --End elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. --Start --Tomb Stoned --Event Listeners bg.enterFrame=scrollBG Runtime:addEventListener("enterFrame", bg) bg2.enterFrame=scrollBG2 Runtime:addEventListener("enterFrame", bg2) stone1.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone1) stone2.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone2) stone3.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone3) stone4.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone4) stone5.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone5) --Ghost Listener Runtime:addEventListener("touch", jump) --Coin Move coin.enterFrame=scrollCoin Runtime:addEventListener("enterFrame", coin) coin.collision=onCoinCollision coin:addEventListener("collision", coin) --Coin Move coin2.enterFrame=scrollCoin2 Runtime:addEventListener("enterFrame", coin2) coin2.collision=onCoin2Collision coin2:addEventListener("collision", coin2) --End end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view ("sceneGroup"). -- Insert code here to clean up the scene. -- Example: remove display objects, save state, etc. end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

Hi again,

This is a scope issue, which I mentioned in my first post. You’ve created “bg” as a local variable inside the :create() function, but then you try to point to it outside that function. Lua does not know what you’re referring to, since it’s out of scope, and thus it throws the error.

Best regards,

Brent

Hi thank you for the help, I really appreciate it.

I have everything almost working correctly. I have a try again menu that pops up if a user touches a rock. The user has the option to try again but when I load the game scene so they can try again, the scene is loaded with them touching the rock again right where they left off. This make them immediately go back to the try again menu. Is there a way to go to a scene but have the script start over when the scene is loaded?