I have literally been stumped on this section of Upwards and Onwards. I failed at it 3 times straight. As basically all of my games would have multiple scenes, I know I need to learn how to use it- but it’s definitely not easy for me. Does anyone have any advice for noobs or recommended tuts to help with understanding this? Thanks.
Composer is pretty simple actually. :create is called when the scene is not shown, :show is called when that screen is on, :hide is called when you switch the scene. Where are you stumped actually? Maybe I can help.
I wish I could tell you. I still don’t know what I did wrong the last few times. I even looked at the script they give at the very end, and I still can’t figure it out.
A link or sample project would help.
By the way, you don’t really need to know it inside out if you don’t need that specific abilities. I’m using Composer for years now and I don’t really know everything about it. I just use it to switch between scenes and load/show some stuff.
Hey @nerdzmasterz, I understand where you are coming from. My previous coding experience dealt with windows, pages and dialogs - compared to that this whole scene thing had me confused for a bit at first.
To visualize and understand it better, have a look at the Scene Flow/Events section of the Composer Library docs (https://docs.coronalabs.com/guide/system/composer/index.html#scene-flowevents)
The flow chart on the right of the page helped me to really grasp what’s happening & why.
Hope this helps, cheers
Thanks for the help! MadDogDean, I will check out those docs. Meanwhile, I was trying to use Composer again, and still no luck. I can’t seem to make the image for the game pop up at all. Here is the sample code so far.
local composer = require( “composer” )
local scene = composer.newScene()
– Code outside of the scene event functions below will only be executed ONCE unless
– the scene is removed entirely (not recycled) via “composer.removeScene()”
local function gotoGame()
composer.gotoScene( “game” )
end
local function gotoHighScores()
composer.gotoScene( “highscores” )
end
– Scene event functions
– create()
function scene:create( event )
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
local background = display.newImageRect( sceneGroup, “title.png”, 500, 80)
title.x = display.contentCenterX
title.y = 200
local playButton = display.newText( sceneGroup, “Play”, display.contentCenterX, 700, native.systemFont, 44 )
playButton:setFillColor( 0.82, 0.86, 1 )
local highScoresButton = display.newText( sceneGroup, “High Scores”, display.contentCenterX, 810, native.systemFont, 44 )
highScoresButton:setFillColor( 0.75, 0.78, 1 )
end
– show()
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is still off screen (but is about to come on screen)
elseif ( phase == "did" ) then
-- Code here runs when the scene is entirely on screen
end
end
– hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is on screen (but is about to go off screen)
elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
end
end
– destroy()
function scene:destroy( event )
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
end
– Scene event function listeners
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene
What error message appears?
First cause of error is your use of quotation characters. You are using(”) instead of ("). After fixing that, other error is how to place your background object. You named that image background
but tried to position it with the name
title which does not exist.
You can take a look at that code and see the fix: (deneme.lua (1.2 KB) )
local composer = require ("composer")
local scene = composer.newScene()
local sceneGroup
local function changeScene()
end
function scene:create( event )
sceneGroup = self.view
local background = display.newImageRect( sceneGroup, "title.png", 500, 80)
background.x = display.contentCenterX
background.y = 200
local playButton = display.newText( sceneGroup, "Play", display.contentCenterX, 700, native.systemFont, 44 )
playButton:setFillColor( 0.82, 0.86, 1 )
local highScoresButton = display.newText( sceneGroup, "High Scores", display.contentCenterX, 810, native.systemFont, 44 )
highScoresButton:setFillColor( 0.75, 0.78, 1 )
end
function scene:show( event )
local phase = event.phase
if ( phase == "will" ) then
elseif ( phase == "did" ) then
end
end
function scene:hide( event )
local phase = event.phase
if ( phase == "will" ) then
elseif ( phase == "did" ) then
end
end
function scene:destroy( event )
end
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
return scene
@bgmadclown You beat me to it.
I’m not sure on the " though, I think the forum changed those, several of his comments had the “--
” changed to a long dash “–”
I noticed that too with the background using “title.png”. Actually, @nerdzmasterz mashed 2 blocks together.
He wrote
local background = display.newImageRect( sceneGroup, "title.png", 500, 80)
title.x = display.contentCenterX
title.y = 200
but it should have been:
local background = display.newImageRect( sceneGroup, "background.png", 800, 1400 )
background.x = display.contentCenterX
background.y = display.contentCenterY
local title = display.newImageRect( sceneGroup, "title.png", 500, 80 )
title.x = display.contentCenterX
title.y = 200
Also, @nerdzmasterz, be sure to read the error messages in the Corona Simulator Console, not just the pop-up error messages. I didn’t pay attention to the Console in the beginning and was missing out on key error messages.
Cheers
@nerdzmasterz, one other comment. If you post code to the forum, be sure to use the code tag as it makes it easier to read.
[code]
write your beautiful Lua code here
[/code]
and it’ll come out like this
local background = display.newImageRect( sceneGroup, "background.png", 800, 1400 )
-- write your beautiful Lua code here
Cheers
Oh, duh… Thanks! It works now.
Don’t sweat it.
The first time I ran through the tutorial, I accidentally dropped the images in the wrong subfolder and kept getting a “nil value error” - that’s when I learned to read the Simulator Console output
Have fun, cheers!