Composer trouble adding a score as I transition between scenes

I’m creating a very simple point and click game using Composer and having multiple scenes.

I’d like to have the player click on the rectangle (a door) and gain a points. the rectangle is also the trigger that sends the player to the next scene (room). If the player clicks on anything but the rectangle he looses points and remains in the scene (room). I’d like to only reveal the score on the final scene (room).

I’m able to transition from room to room but I can’t figure out how to add a score and have it carry over to the final slide for display.

I’ve been reading a lot on the subject but the information is not always clear on where I say what?

Thanks in advance.

Here is an example of my code:

local composer = require( “composer” )

local scene = composer.newScene()

local score = 0

composer.setVariable( “addToScore”, 100 )


– Code outside of the scene event functions below will only be executed ONCE unless

– the scene is removed entirely (not recycled) via “composer.removeScene()”


– Initialize variables

local options = {

    effect = “fade”,

    time = 50000,

    params = {

        someKey = “someValue”,

        someOtherKey = 10

    }

}

local function gotoScene2()

    composer.gotoScene( “scene2”, “addToScore” )

end

–local function addToScore(num)

    --score = score + 10

    

–end

local function addToScore(num)

    score = score + 200

    scoreText.text = "Score: " … score

    print(score)

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 image1 = display.newImageRect(“Slide001.png”, display.contentWidth, display.contentHeight) 

    image1.anchorX = 0

    image1.anchorY = 0

    image1: toBack( )

    local rectangle1 = display.newRect( 110, 290, 100, 100 )

    rectangle1.alpha = .1

    

    rectangle1:addEventListener ( “tap”, gotoScene2, onTapped )

end

Are you trying to pass variables to another scene? If so, here it is: https://docs.coronalabs.com/api/library/composer/gotoScene.html#scene-options

or this: https://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/

I’m trying to do that. But It’s not working. I’m reading the information the link and trying to apply it to my code but I’m not sure how to just that.

I’m still having trouble adding a score and then transferring that score to the next scene.

Have you looked at the links I’ve sent? Your code is nowhere near the syntax that’s on the docs.

For example:

This

local options = { effect = "fade", time = 50000, params = { someKey = "someValue", someOtherKey = 10 } } local function gotoScene2() composer.gotoScene( "scene2", "addToScore" ) end --local function addToScore(num) --score = score + 10 --end local function addToScore(num) score = score + 200 scoreText.text = "Score: " .. score print(score) end

is NOT the same with what’s on the docs:

local currentScore = 200 -- Later... local options = { effect = "fade", time = 800, params = { level="Level 1", score=currentScore } } composer.gotoScene( "results", options )

Yes I did write that I’ve been reading it, but I’ve been having trouble adapting it. For example, the “someValue” line is a bit confusing as I’m not sure what to place there.

I even tried placing your example in y code but keep getting errors when I try to go to the next scene.

As I’ve said, I’ve read it but I’m having trouble adapting it to my code.

The thing is, you are trying to push a function addToScore to composer.gotoScene() which asks you for a table. In your case, your code should look something like this:

local options = { effect = "fade", time = 50000, -- this is 50 seconds by the way params = { someKey = "someValue", someOtherKey = 10 } } local function gotoScene2() composer.gotoScene( "scene2", options ) end

with some modification to send the score to the other scene:

local options = { effect = "fade", time = 50000, -- this is 50 seconds by the way params = { someKey = "someValue", someOtherKey = 10, scoreToPass = score, -- score is the one you'll calculate before calling this } }

after doing that, you’ll get those values from event.params in the other scene:

function scene:create( event ) -- whatever you do.. local score = event.params.scoreToPass -- whatever you do.. end

Ok, I’ve tried cleaning my code and now have the following but I’m still not sure how to add a score that goes from scene to scene?

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()”


– Initialize variables

local function gotoScene2()

    composer.gotoScene( “scene2”, { time=800, effect=“crossFade” } )

end


– Scene event functions


– create()

function scene:create( event )

    sceneGroup = self.view

    – Code here runs when the scene is first created but has not yet appeared on screen

    local image1 = display.newImageRect(“Slide001.png”, display.contentWidth, display.contentHeight) 

    sceneGroup:insert( image1 )

    image1.anchorX = 0

    image1.anchorY = 0

    image1:toBack( )

    local rectangle1 = display.newRect( 110, 290, 100, 100 )

    sceneGroup:insert( rectangle1 )

    rectangle1.alpha = .1

    rectangle1:addEventListener ( “tap”, gotoScene2,{ time=800, effect=“crossFade” } )

end

Can you take a look at the example I provided above?

I am now trying to add your example to the code I have just “cleaned up” but I keep getting errors.

Can you post the final code? Please use <> symbol and paste the code to make it readable in forums, please.

here is my code thus far, I get errors with this line <

 local score = event.params.scoreToPass

here is the whole code thus far:

-- Initialize variables local options = { effect = "fade", time = 800, params = { someKey = "someValue", someOtherKey = 10, scoreToPass = score, } } local function gotoScene2() composer.gotoScene( "scene2", options ) end -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() function scene:create( event ) sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen local image1 = display.newImageRect("Slide001.png", display.contentWidth, display.contentHeight) sceneGroup:insert( image1 ) image1.anchorX = 0 image1.anchorY = 0 image1:toBack( ) local rectangle1 = display.newRect( 110, 290, 100, 100 ) sceneGroup:insert( rectangle1 ) rectangle1.alpha = .1 local score = event.params.scoreToPass rectangle1:addEventListener ( "tap", gotoScene2 ) end

You need to get that line into scene2 file to get the score you pass from scene1.

I just put it in scene2 (not sure where in the scene so I’m trying different places. But it’s still giving errors.

Here is the best I can do for you:

main.lua

local composer = require ("composer") composer.gotoScene("scene1", "crossFade", 1000)

scene1.lua

local composer = require ("composer") local scene = composer.newScene() local mainGroup local scorePlayer = 0 local function changeScene() local options = { effect = "crossFade", time = 1000, params = {scoreToPass = scorePlayer} } composer.gotoScene( "scene2", options ) end function scene:create( event ) mainGroup = self.view end function scene:show( event ) local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then scorePlayer = scorePlayer + 50 -- increase score changeScene() 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 scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

scene2.lua

local composer = require ("composer") local scene = composer.newScene() local mainGroup function scene:create( event ) mainGroup = self.view local score = event.params.scoreToPass print ("score is passed", score) 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 scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

Wow! thank you.

I’m just curious where I put the code for my image and square and square_AddEventListner?

I guess that’s where you’d want them to be!

(You should definitely check tutorials and docs about composer. I don’t think you got the hold of it.)

Sorry, I’m confussed, the code you provided is just part of the code? I still don’t understand the order to place things in. If I add your code and mine I get errors. I’ve been reading the docs for a week now for this project and I find that it’s not always clear what line goes where in what file.

Not sure how the system is now going to load the following or where to place them.

No matter where I place my lines I get errors

I have been reading the documentation on Composer but again, not very clear on what goes where (it will give a line and then the next line state “late” but does not specify if “later” is in the same document or the second document.

I’ve been rewriting this code over and over, I’ve started over several times in the past week and still cannot find a simple way to add scores and have them got from scene to scene.

-- Initialize variables local function gotoScene2() composer.gotoScene( "scene2", { time=800, effect="crossFade" } ) end -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() function scene:create( event ) mainGroup = self.view end sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen local image1 = display.newImageRect("Slide001.png", display.contentWidth, display.contentHeight) sceneGroup:insert( image1 ) image1.anchorX = 0 image1.anchorY = 0 image1:toBack( ) local rectangle1 = display.newRect( 110, 290, 100, 100 ) sceneGroup:insert( rectangle1 ) rectangle1.alpha = .1 rectangle1:addEventListener ( "tap", gotoScene2 ) end

It is just a sample code for you to understand how data is passed from scene1 to scene2.

There is no point in telling you which line goes where since it’s your choice to include those code wherever you like. If you want to increase the score when a button is tapped, go ahead and increment the score by calling a function on “touch” or if you like, increment the score with a timer as time passes by. If you are having problem with tasks like that, it may be a good idea to revise your programming knowledge before going in further.

By the way, it’s explained in detail how basic composer template works, in this tutorial: https://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/

I have read that tutorial and others like it. That’s why I’m here because the answers in the tutorials are not clear or I’m trying something different. It’s not creating the scene itself that is giving me problems. My program works. It’s when I try to add a score on my square tap that things don’t work. The score will also not transfer to the next slide. this was my original request “Trouble add a score”. I’ve tried all the example and whenever I add them to my code I get errors so I don’t understand being able to add them anywhere.

Let me add a little detail to those two files:

scene1.lua

local composer = require ("composer") local scene = composer.newScene() local mainGroup local scorePlayer = 0 local textScore local function changeScene() local options = { effect = "crossFade", time = 1000, params = {scoreToPass = scorePlayer} -- parameters to pass } composer.gotoScene( "scene2", options ) end local function increaseScore(event) if (event.phase == "began") then scorePlayer = scorePlayer + 50 -- increase score textScore.text = scorePlayer -- show increased score if (scorePlayer \> 200) then -- if player scored enough, change scene changeScene() end end return true end function scene:create( event ) mainGroup = self.view local rectScore = display.newRect(display.contentCenterX, 450, 100, 100) -- push this rectangle to increase score mainGroup:insert(rectScore) rectScore:addEventListener("touch", increaseScore) textScore = display.newText( scorePlayer, display.contentCenterX, display.contentCenterY, native.systemFontBold, 64 ) -- show the current score mainGroup:insert( textScore ) 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 scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

scene2.lua

local composer = require ("composer") local scene = composer.newScene() local mainGroup function scene:create( event ) mainGroup = self.view local score = event.params.scoreToPass local textScore = display.newText( "score passed " .. score, display.contentCenterX, display.contentCenterY, native.systemFontBold, 64 ) mainGroup:insert( textScore ) 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 scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene