How to insert score into localGroup so I can remove it in Composer and Android Back Button?

so in my new game basically i require score.lua and highscore.lua to show up score in the screen. when the player get hit by certain trap, game.lua will make transition (with composer) to gameover.lua where I have this code in gameover.lua:

local highScoreGambar = require ("highscore") local scoreGambar = require ("score")

basically scoreGambar is the display object where I will show the numbers in score.lua into this gameover.lua. Same thing with highScoreGambar (for highscore.lua)

this is what I have in score.lua:

-- A sample score keeping display -- This updates a display for a numeric score -- Example usage: -- Place the score at 50,50 -- score.init( { x = 50, y = 50 } ) -- Update the score to current value + 10: -- score.setScore( score.getScore() + 10 ) module(..., package.seeall) local composer = require( "composer" ) -- Init images. This creates a map of characters to the names of their corresponding images. local numbers = { [string.byte("0")] = "0.png", [string.byte("1")] = "1.png", [string.byte("2")] = "2.png", [string.byte("3")] = "3.png", [string.byte("4")] = "4.png", [string.byte("5")] = "5.png", [string.byte("6")] = "6.png", [string.byte("7")] = "7.png", [string.byte("8")] = "8.png", [string.byte("9")] = "9.png", [string.byte(" ")] = "space.png", } -- score components local theScoreGroup local theBackgroundBorder local numbersGroup local theScore -- the location of the score image -- initialize the score -- params.x \<= X location of the score -- params.y \<= Y location of the score function init( params ) theScoreGroup = display.newGroup() theBackgroundBorder = 10 numbersGroup = display.newGroup() theScoreGroup:insert( numbersGroup ) theScore = 0 theScoreGroup.x = params.x theScoreGroup.y = params.y setScore( 0 ) end -- update display of the current score. -- this is called by setScore, so normally this should not be called function update() -- remove old numerals theScoreGroup:remove(1) local numbersGroup = display.newGroup() theScoreGroup:insert( numbersGroup ) -- go through the score, right to left local scoreStr = tostring( theScore ) local scoreLen = string.len( scoreStr ) local i = scoreLen -- starting location is on the right. notice the digits will be centered on the background local x = theScoreGroup.contentWidth - theBackgroundBorder local y = theScoreGroup.contentHeight / 2 for a=1,i do -- fetch the digit local c = string.byte( scoreStr, a ) local digitPath = numbers[c] local characterImage = display.newImage( digitPath ) -- put it in the score group numbersGroup:insert( characterImage ) -- place the digit characterImage.x = x + characterImage.width / 2 characterImage.y = y x = x + characterImage.width -- --i = i - 1 end end -- get current score function getScore() return theScore end -- set score to value -- score \<= score value function setScore( score ) theScore = score update() end function remove() theScoreGroup.x = 1000000 theScoreGroup.y = 1000000 end

I successfully created a button where (if it get touched) the player will go directly to menu.lua from gameover.lua, this is the code for the button:

local function onClickdiffusemainmenu (event) if event.phase == "ended" then audio.stop (gameMusicChannel) scoreGambar.remove() highScoreGambar.remove() destroyTrigger = "mainmenu" composer.removeScene( "gameover" ) end end diffusemainmenu:addEventListener ("touch", onClickdiffusemainmenu)

thing is, the same code won’t work for android physical back button. I do realize that android back button codes should be written in main.lua and not in every scene at composer, so I have this code at main.lua:

function onKeyEvent( event ) message = "Key '" .. event.keyName .. "' was pressed " .. event.phase print( message ) if (event.keyName == "back" and event.phase == "up") or ("b" == keyName and phase == "up" and system.getInfo("environment") == "simulator") then if composer.getSceneName("current") == "menu" then native.requestExit( ) elseif composer.getSceneName("current") == "howtoplay" then composer.removeScene("howtoplay") composer.gotoScene("menu", "fade", 400) elseif composer.getSceneName("current") == "gameover" then composer.removeScene("gameover") composer.gotoScene("menu", "fade", 400) end return true end return false end Runtime:addEventListener( "key", onKeyEvent );

with this code, if I touch android physical back button on gameover.lua, I do get redirected to menu.lua BUT the score and highscore numbers are still there in menu.lua (which they shouldn’t). I simply cant remove them. I tried to insert scoreGambar and highScoreGambar to localGroup but it returned usual pop up error message in corona simulator. I strongly believe I need to add something to localGroup. because when “something” doesnt get removed when we change scene, it’s usually linked to that. but how? I dont know what to add to localGroup so I can remove these “scores”

hello, anyone? Thanks.

Looking at score.lua you are never returning a display object to the calling code, so you can’t insert it that way.  You could pass the scene’s group in as a parameter when you call your score.init() and the have your score code insert it into the group since you would have a reference to it.

Rob

i dont really know how to do that actually, tried several different ways but always returned error whenever my gameover is calling score.lua can you be more specific? Thank you

Maybe change your init function to:

-- initialize the score -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params.x \<= X location of the score -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params.y \<= Y location of the score --&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; params.sceneGroup = scene to insert the group into function init( params ) &nbsp;&nbsp;&nbsp;&nbsp;theScoreGroup = display.newGroup() &nbsp;&nbsp;&nbsp;&nbsp;theBackgroundBorder = 10 &nbsp;&nbsp;&nbsp;&nbsp;numbersGroup = display.newGroup() &nbsp;&nbsp;&nbsp;&nbsp;theScoreGroup:insert( numbersGroup ) &nbsp;&nbsp;&nbsp;&nbsp;theScore = 0 &nbsp;&nbsp;&nbsp;&nbsp;theScoreGroup.x = params.x &nbsp;&nbsp;&nbsp;&nbsp;theScoreGroup.y = params.y &nbsp;&nbsp;&nbsp;&nbsp;setScore( 0 ) &nbsp;&nbsp;&nbsp; params;sceneGroup:insert( theScoreGroup ) end

Then somewhere in your code you’re calling that .init() function.  Pass in the sceneGroup as one of your parameters when you call it.

Rob

ok thanks, it works now
 

hello, anyone? Thanks.

Looking at score.lua you are never returning a display object to the calling code, so you can’t insert it that way.  You could pass the scene’s group in as a parameter when you call your score.init() and the have your score code insert it into the group since you would have a reference to it.

Rob

i dont really know how to do that actually, tried several different ways but always returned error whenever my gameover is calling score.lua can you be more specific? Thank you

Maybe change your init function to:

-- initialize the score -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params.x \<= X location of the score -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params.y \<= Y location of the score --&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; params.sceneGroup = scene to insert the group into function init( params ) &nbsp;&nbsp;&nbsp;&nbsp;theScoreGroup = display.newGroup() &nbsp;&nbsp;&nbsp;&nbsp;theBackgroundBorder = 10 &nbsp;&nbsp;&nbsp;&nbsp;numbersGroup = display.newGroup() &nbsp;&nbsp;&nbsp;&nbsp;theScoreGroup:insert( numbersGroup ) &nbsp;&nbsp;&nbsp;&nbsp;theScore = 0 &nbsp;&nbsp;&nbsp;&nbsp;theScoreGroup.x = params.x &nbsp;&nbsp;&nbsp;&nbsp;theScoreGroup.y = params.y &nbsp;&nbsp;&nbsp;&nbsp;setScore( 0 ) &nbsp;&nbsp;&nbsp; params;sceneGroup:insert( theScoreGroup ) end

Then somewhere in your code you’re calling that .init() function.  Pass in the sceneGroup as one of your parameters when you call it.

Rob

ok thanks, it works now