Composer trouble adding a score as I transition between scenes

I see what you are writing but it still will not let me do this with my code. I still have to add my image and rectangle.

What I’m basically trying to do it a simple point and click game (super simple version of Myst) 

When the player clicks on the rectangle (door) s/he is taken to the next room and gains points. If the player clicks anything else in the image some points are taken away.

I’ve designed my background images, created transparent touch points that go with my images. I have coded going to the next room (I have 11 total) I’m still stuck trying to add/subtract the score and have the score pass to the next room (I’d like to only display the score when the game is over.)

I’m going through your code again, but still getting errors whenever I try to add any form of it to my game. I notce my recent error ws with the lines:

sceneGroup:insert( image2 )

Whenever I add my background image. So I removed it and then only my scene one image was visible throughout the game. I found by placing this line after the rectangle creation it seemed to work.

That’s a variation of the code I’ve posted above. You only need to call changeScene() when you click the rectangle and then increase the score in this function before composer.gotoScene() call.

Yes, that part I figured out, and it’s genius, thank you.

I have it working going from scene1 to scene2, now I’m trying to replicate to go to scene3 but I get errors in"function gotoscene" and “gotoscene2”

local function changeScene() local options = { effect = "crossFade", time = 1000, params = {scoreToPass = scorePlayer} } composer.gotoScene( "scene3", options ) -- \<-- I'm getting an error with this line. end local function increaseScore(event) if (event.phase == "began") then scorePlayer = scorePlayer + 10 -- show increased score if (scorePlayer \> 5) then changeScene() -- \<-- I'm getting an error with this line. end end return true end 

this is stopping me from going to the next scene.

@richtornado, I already wrote you extensive code to handle your images and rect click logic dynamically, it also included a basic score system - see this.

I’m not being mean, but if you are not grasping the help that @madclown is giving you then perhaps you are trying to do something in advance of your skill set and you need to focus on more basic examples to get a better understanding of how things work. To run you must first crawl and then walk.

A side note: although generally global variables are bad form that doesn’t mean you shouldn’t use them!  I use globals a lot - mainly class references to large data sets - that would make very cryptic code always passing them around.  Let me explain…

I generally have a “game stats” global class that stores all my run time variables.  This class will be available to all modules in my app.  Each variable is encapsulated in GET / SET style coding to ensure proper data validation and integrity.  To elaborate…

Assuming you had a player class and that had a variable called player.score.  Never allow direct access to that variable (as that is when bugs occur) so instead have two functions like player.setScore() and player.getScore().  What they do should be obvious.

I do realize that this project is a bit out of my scope and I do appreciate the help from @madclown. i actually learn more from this kind of doing and tinkering. I don’t think I’ll take on another project this large for a while. I just have to have this one finished for a special project.

As I’ve practiced with the codes given I’ve actually learned more these past couple of days. For that I am truly grateful.

But I’m tinkering and learning what does what. (Some forks are more hands on, sorry)

I think I solved my own problem: my “score” was set to local.

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?