Updating score when using composer

Hi all,

I am trying to work my way through a few tutorials and am in the process of putting them all together. I am currently working on the score system which is easy to get going on a one screen game. However I cannot workout how to update the score when using composer.

I have created the text and the score variable but I cannot work out in which scene to place the function to update the score. Currently using the below code the score updates and prints in console but does not update in the game. I figure this is because score is not in the correct scene section and is therefore not updating in realtime but wherever I put it in runs in to problems. 

Any help would be greatly appreciated.

local composer = require( "composer" ) local widget = require("widget") local scene = composer.newScene() -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- -- local forward references should go here 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 bg local backBtn local scenery local cloud local tree local transition1 local scoreText local score = 0 local actualScore -- ------------------------------------------------------------------------------- ---Create Functions local function addToScore(num) score = score + num print(score) end local function goSomewhere(event) local goto = event.target.id local options = {effect= "fade", time = 200} composer.gotoScene( goto, options ) end local function onTapped(event) local num = math.random( 500 ) local obj = event.target print(obj.name .. " tapped") addToScore(num) return true end local function onTouched(event) local obj = event.target if event.phase == "began" then display.getCurrentStage():setFocus(obj) obj.startMoveX = obj.x obj.startMoveY = obj.y --print( obj.name .. " touched" ) elseif event.phase == "moved" then obj.x = (event.x - event.xStart) + obj.startMoveX obj.y = (event.y - event.yStart) + obj.startMoveY elseif event.phase == "ended" or event.phase == "cancelled" then display.getCurrentStage():setFocus(nil) end return true end local function driftAcross(obj) local function resetObj(obj) cloud.x = screenLeft - cloud.width transition.to(obj, { alpha=1, y=screenTop + 60,onComplete=driftAcross} ) end local transition1 = transition.to( obj, { time = 20000, x =screenRight+cloud.width, alpha=0.3, onComplete=resetObj } ) end local function setupDisplay(grp) scenery = display.newImage("images/background1.png") scenery.width = screenWidth scenery.height = screenHeight scenery.x = centerX scenery.y = centerY scenery.name = "scenery" grp:insert(scenery) -- create foreground objects cloud = display.newImage("images/cloud1.png") cloud.name = "cloud" grp:insert(cloud) tree = display.newImage("images/tree.png") tree.name = "tree" grp:insert(tree) backBtn = widget.newButton ({label="Back", id="menu", onRelease=goSomewhere}) backBtn.anchorX = 0 backBtn.anchorY = 1 backBtn.x = screenLeft backBtn.y = screenBottom grp:insert(backBtn) scoreText = display.newText( "Score:", centerX-60, screenBottom-20, "Helvetica", 20 ) scoreText:setFillColor( 1,0.2,0.2) grp:insert(scoreText) actualScore = display.newText(score, scoreText.x+scoreText.width\*0.5, scoreText.y, "Helvetica", 23 ) actualScore.anchorX=0 actualScore.anchorY=0.5 grp:insert(actualScore) tree:addEventListener( "touch", onTouched ) cloud:addEventListener( "touch", onTouched ) tree:addEventListener( "tap", onTapped ) cloud:addEventListener( "tap", onTapped ) scenery:addEventListener( "tap", onTapped ) end -- "scene:create()" function scene:create( event ) local sceneGroup = self.view setupDisplay(sceneGroup) -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then tree.x = screenRight - 100 tree.y = screenBottom - 80 cloud.x = screenLeft- cloud.height cloud.y = screenTop + 60 cloud.alpha = 1 score = score \* 0 print(score) -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then driftAcross(cloud) --onTapped(event) Does not work here -- 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 transition.cancel( transition1 ) -- 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. elseif ( phase == "did" ) then --composer.removeScene( "play", true ) --print( "scene removed" ) 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. end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

Hi Mark,

Is the main issue that you want to keep track of the same score from scene to scene? The method can vary from developer to developer, but one of the easiest ways is to use Composer’s variable functions, where you can keep track of variables across the entire Composer library:

http://docs.coronalabs.com/api/library/composer/setVariable.html

http://docs.coronalabs.com/api/library/composer/getVariable.html

Hope this helps,

Brent

Hi Brent,

Thanks for the reply. Sorry reading back I realised I probably haven’t described my issue correctly.

On loading into the play.lua file from my menu.lua file i have the score text displaying correctly as 0. However when I “touch” the tree, which i have setup to add math.random(500) to the score, it doesn’t update the score text on the screen. When I print the score in the console (from the addToScore() function) the updated score shows up correctly but it doesnt update on the screen. As I said i suspect this is because the score is only being created in the scenecreate section of composer but I cannot work out how to update the score once the scene is loaded. Hope this makes more sense.

Thanks,

Mark 

Mark, you can change your addToScore() function like this and it should work:

local function addToScore(num) score = score + num scoreText.text = "Score: " .. score print(score) end

 Jay

[EDIT: When you create the scoreText object there is a property called text that can be used to update the text shown by that object.]

Thanks Jay & Brent, thought I tried this but obviously I didnt, but this works now.

Thanks again.

Hi Mark,

Is the main issue that you want to keep track of the same score from scene to scene? The method can vary from developer to developer, but one of the easiest ways is to use Composer’s variable functions, where you can keep track of variables across the entire Composer library:

http://docs.coronalabs.com/api/library/composer/setVariable.html

http://docs.coronalabs.com/api/library/composer/getVariable.html

Hope this helps,

Brent

Hi Brent,

Thanks for the reply. Sorry reading back I realised I probably haven’t described my issue correctly.

On loading into the play.lua file from my menu.lua file i have the score text displaying correctly as 0. However when I “touch” the tree, which i have setup to add math.random(500) to the score, it doesn’t update the score text on the screen. When I print the score in the console (from the addToScore() function) the updated score shows up correctly but it doesnt update on the screen. As I said i suspect this is because the score is only being created in the scenecreate section of composer but I cannot work out how to update the score once the scene is loaded. Hope this makes more sense.

Thanks,

Mark 

Mark, you can change your addToScore() function like this and it should work:

local function addToScore(num) score = score + num scoreText.text = "Score: " .. score print(score) end

 Jay

[EDIT: When you create the scoreText object there is a property called text that can be used to update the text shown by that object.]

Thanks Jay & Brent, thought I tried this but obviously I didnt, but this works now.

Thanks again.