Composer trouble adding a score as I transition between scenes

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

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.