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”