How do I Convert My Game Into Composer?

I have a game that is mostly done, and I want to put it into composer before I finish it. I have the complete game code and a composer template that both work individually, however, I cannot seem to combine them, I have tried many different layouts but could not seem to make it work. 

My game code is 

display.setStatusBar(display.HiddenStatusBar) local composer = require( "composer" ) composer.gotoScene( "menu" ) local composer = require("composer") composer.gotoScene("menu") -- Setup and start physics local physics = require("physics") physics.start() physics.setGravity(0,1) -- most commonly used screen coordinates centerX = display.contentCenterX centerY = display.contentCenterY screenLeft = display.screenOriginX screenWidth = display.contentWidth - screenLeft \* 2 screenRight = screenLeft + screenWidth screenTop = display.screenOriginY screenHeight = display.contentHeight - screenTop \* 2 screenBottom = screenTop + screenHeight display.contentWidth = screenWidth display.contentHeight = screenHeight --Variables ballRadius=20 balls={} numberOfBalls = 0 tick=400 text = {} numberOfText = 0 local deltaX = screenWidth/4 ball1x = deltaX bally = screenTop - 60 ball2x = deltaX\*2 ball3x = deltaX\*3 tick = 400 gameloop = 0 livesCounter = 0 lives = 3 loss = 0 on = true tapped = true display.setDefault( "background", 1, 1, 0.9 ) local bottom = display.newRect(centerX, screenBottom, screenWidth,40) bottom:setFillColor(1, 1, 0.9) physics.addBody(bottom,"static") local DisplayLivesLabel = display.newText("lives: ", screenLeft + 50, screenTop + 50, native.systemFont, 16 ) DisplayLivesLabel:setFillColor(1,0,0) --local DisplayLives = display.newText( tostring( lives ), screenLeft + 75, screenTop + 50, native.systemFont, 16 ) --DisplayLives:setFillColor(1,0,0) -- Math Equation local function Equation() local v1 = math.random(1,30) local v2 = math.random(1,30) ans = v1 + v2 local questions = (v1.." + "..v2) questionText = display.newText((questions), centerX, centerY, native.systemFont, 16 ) questionText:setFillColor( 1, 0, 0 ) end -- Creates Balls 1 to 3 local function CreateBall1() ball1 = display.newCircle(ball1x, bally, 30 ) ballText = display.newText(ans, ball1x, bally) ball1:setFillColor(math.random(0, 255)/255, math.random(0, 255)/255, math.random(0, 255)/255) physics.addBody( ball1,{ "dynamic", density=1.0, friction=0.3, bounce=0.2, radius=25 } ) physics.addBody( ballText,{ "dynamic", density=1.0, friction=0.3, bounce=0.2, radius=25 } ) end local function CreateBall2() ball2 = display.newCircle(ball2x, bally, 30 ) ball2Text = display.newText(ans + math.random(1,6), ball2x, bally) ball2:setFillColor(math.random(0, 255)/255, math.random(0, 255)/255, math.random(0, 255)/255) physics.addBody( ball2,{ "dynamic", density=1.0, friction=0.3, bounce=0.2, radius=25 } ) physics.addBody( ball2Text,{ "dynamic", density=1.0, friction=0.3, bounce=0.2, radius=25 } ) end local function CreateBall3() ball3 = display.newCircle(ball3x, bally, 30 ) ball3Text = display.newText(ans - math.random(1,6), ball3x, bally) ball3:setFillColor(math.random(0, 255)/255, math.random(0, 255)/255, math.random(0, 255)/255) physics.addBody( ball3,{ "dynamic", density=1.0, friction=0.3, bounce=0.2, radius=25 } ) physics.addBody( ball3Text,{ "dynamic", density=1.0, friction=0.3, bounce=0.2, radius=25 } ) end -- Destroys the Balls local function EndBall1() ball1:removeSelf() ballText:removeSelf() end local function EndBall2() ball2:removeSelf() ball2Text:removeSelf() end local function EndBall3() ball3:removeSelf() ball3Text:removeSelf() end -- Ends the Equation local function EquationEnd() questionText:removeSelf() end local function reset() EndBall1() EndBall2() EndBall3() EquationEnd() Equation() CreateBall1() CreateBall2() CreateBall3() tapped = true buffer = true -- DisplayLives() end local function OnTap1(event) local correct = display.newText("CORRECT", centerX, centerY, native.systemFont, 36 ) correct:setFillColor( 0,0.7, 0 ) reset() ball3:addEventListener("tap", OnTap2) ball2:addEventListener("tap", OnTap2) ball1:addEventListener("tap", OnTap1) end local function gameover() end local function resetlives() lives = 3 end local function loseLife() if buffer == true then lives = lives - 1 buffer = false reset() loseLife() if lives == 0 then resetlives() gameover() end end end -- If the wrong one is pressed local function OnTap2(event) if tapped == true then ball3:removeEventListener("tap", OnTap2) ball2:removeEventListener("tap", OnTap2) tapped = false local incorrect = display.newText("INCORRECT", centerX, centerY, native.systemFont, 36 ) incorrect:setFillColor( 0.7,0, 0 ) lives = lives - 1 DisplayLivesLabel.text = ("lives: " .. lives) reset() incorrect:removeSelf() ball3:addEventListener("tap", OnTap2) ball2:addEventListener("tap", OnTap2) ball1:addEventListener("tap", OnTap1) end end -- If the correct one is pressed -- Restart the Game -- Creating Box local function Box() local QBox = display.newRect(centerX,centerY,200,75) QBox.strokeWidth = 3 QBox:setFillColor(1) QBox:setStrokeColor(0) end -- Starts the Game local function GameStart() if gameloop == 0 then Box() Equation() CreateBall1() CreateBall2() CreateBall3() reset() ball3:addEventListener("tap", OnTap2) ball2:addEventListener("tap", OnTap2) ball1:addEventListener("tap", OnTap1) end end local function onCollision(event) reset() OnTap2() end -- Loops the game and the event listeners local function gameLoop(event) GameStart() gameloop = 1 end -- game loop timer local timer1 = timer.performWithDelay( 100, gameLoop, 0)

and my composer is linked to another scene(menu), however here is the code that I would like to implement my game into.

local composer = require( "composer" ) local scene = composer.newScene() local widget = require("widget") widget.setTheme ( "widget\_theme\_ios" ) -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- -- most commonly used screen coordinates local centerX = display.contentCenterX local centerY = display.contentCenterY local screenLeft = display.screenOriginX local screenWidth = display.viewableContentWidth - screenLeft \* 2 local screenRight = screenLeft + screenWidth local screenTop = display.screenOriginY local screenHeight = display.viewableContentHeight - screenTop \* 2 local screenBottom = screenTop + screenHeight local levelNum = 0 -- ------------------------------------------------------------------------------- local function goSomewhereX(event) local goto = event.target.id local options = {effect="fromTop", time=300, isModal=true} composer.showOverlay( goto, options ) end local function goSomewhere(event) local goto = event.target.id local options = {} if goto == "pause" then options = {effect="fromTop", time=300, isModal=true} composer.showOverlay( goto, options ) else options = {effect="crossFade", time=300} composer.showOverlay( goto, options ) end end local function setUpDisplay(grp) local bg = display.newRect( grp, screenLeft, screenTop, screenWidth, screenHeight ) bg.x = centerX bg.y = centerY bg:setFillColor(0.19, 0.56, 1) local backBtn = widget.newButton ({ label="\<", id="menu", width=20, height=20, labelXOffset=5, onRelease=goSomewhere }) backBtn.anchorX = 0 backBtn.anchorY = 0 backBtn.x = screenLeft + 10 backBtn.y = screenTop + 10 grp:insert(backBtn) end -- "scene:create()" function scene:create( event ) local sceneGroup = self.view -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. setUpDisplay(sceneGroup) 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). levelNum = composer.getVariable( "levelNum" ) 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. print("play scene:hide will") elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. print("play scene:hide did") 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. print("play scene:destroy") end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

Any help is greatly appreciated.

If everything is done in a single scene then you don’t need composer

Are you saying you want to wrap your game in a framework with:

  • splash screen
  • main menu
  • play gui

and so on?

If yes, I’d convert that game code into a module that can be:

  • created
  • started
  • stopped
  • destroyed

Then, once you have that working well.  Use one of the examples that comes with Corona or one from my collection:

https://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/composer_scene_manager.zip

Index to collection (see #5):

https://github.com/roaminggamer/CoronaGeek/blob/master/Hangouts/composer_scene_manager/README.md

yes, I do want to wrap the code in the framework, however, the problem is that I am unable to do so, any help regarding where in the framework such as where I should place certain parts of my code. e.g Where do I put the variables and functions of the main code into the framework?

I understand that the code works, but when I try and call the original game from a separate page, there are many errors, I have been told this relates to the fact that the scene doesn’t follow the format of

  • Create
  • Start
  • Stop
  • Destroy

 So I have found a template that works, however, the problem is that I don’t know where to place the code into the framework.

Hope this clears things up.

Thanks.

Please see our Getting Started Guide, in particular Chapters 4 and 5.

https://docs.coronalabs.com/guide/programming/04/index.html

Read that one then follow the link to Chapter 5 at the end.

We start with a game that’s all in main.lua and talk you through creating a basic Composer based framework and then how to move things into the game scene.

Rob

Also, read this chart: 

https://docs.coronalabs.com/guide/system/composer/index.html#scene-flowevents

It was very helpful for me, when I ran into coding problems with composer.

If everything is done in a single scene then you don’t need composer

Are you saying you want to wrap your game in a framework with:

  • splash screen
  • main menu
  • play gui

and so on?

If yes, I’d convert that game code into a module that can be:

  • created
  • started
  • stopped
  • destroyed

Then, once you have that working well.  Use one of the examples that comes with Corona or one from my collection:

https://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/composer_scene_manager.zip

Index to collection (see #5):

https://github.com/roaminggamer/CoronaGeek/blob/master/Hangouts/composer_scene_manager/README.md

yes, I do want to wrap the code in the framework, however, the problem is that I am unable to do so, any help regarding where in the framework such as where I should place certain parts of my code. e.g Where do I put the variables and functions of the main code into the framework?

I understand that the code works, but when I try and call the original game from a separate page, there are many errors, I have been told this relates to the fact that the scene doesn’t follow the format of

  • Create
  • Start
  • Stop
  • Destroy

 So I have found a template that works, however, the problem is that I don’t know where to place the code into the framework.

Hope this clears things up.

Thanks.

Please see our Getting Started Guide, in particular Chapters 4 and 5.

https://docs.coronalabs.com/guide/programming/04/index.html

Read that one then follow the link to Chapter 5 at the end.

We start with a game that’s all in main.lua and talk you through creating a basic Composer based framework and then how to move things into the game scene.

Rob

Also, read this chart: 

https://docs.coronalabs.com/guide/system/composer/index.html#scene-flowevents

It was very helpful for me, when I ran into coding problems with composer.