ok so I’ve implemented a high score from the high score tutorial which uses the score.lua file , but i need to use it for a fruit ninja style game so i want it to tell the user there score as there playing and then when they hit a bomb it shows a replay screen where i want there highscore to show , but when they press replay and the game starts again , the score follows on from there last score , so if you have 4 consecutive goes it will keep carrying the score over rather than starting the current score again and saving the high score
this is the code i have used
local score = require( "score" ) function loadScore( event ) if event.phase == "ended" then local prevScore = score.load() if prevScore then score.set(prevScore) end end return true end function chopFruit(fruit) if (sampleVar == true) then score.add(1) playRandomChoppedSound() createFruitPiece(fruit, "top") createFruitPiece(fruit, "bottom") createSplash(fruit) createGush(fruit) fruit:removeSelf() else end end
local function main() display.setStatusBar( display.HiddenStatusBar ) setUpBackground() --livesLabel = display.newText("lifeCounter: ", \_W\*0.3,\_H\*0.3, native.systemFont, 16) --livesText = display.newText("Lives: 0", \_W \*0.3, \_H\*0.3, "Pacifico", 22) --livesText:setTextColor(1, 1, 1) --livesText.text = ("Lives: " )..lives pauseAndResume () setUpCatchPlatform() initGroups() initFruitAndSplash() Runtime:addEventListener("touch", drawSlashLine) startGame() local scoreText = score.init({ fontSize = 32, font = "Pacifico", x = \_W \* 0.5, y = \_H \* 0.15, maxDigits = 5, leadingZeros = true, filename = "scorefile.txt", }) end
function displayGameOver() -- Will return a group so that we can set the alpha of the entier menu group = display.newGroup() -- Dim the background with a transperent square local scoreText1 = score.init({ fontSize = 32, font = "Pacifico", x = \_W \* 0.63, y = \_H \* 0.49, maxDigits = 4, leadingZeros = true, filename = "scorefile.txt", }) saveButton = widget.newButton({ width = 200, height = 64, x = display.contentCenterX, y = display.contentHeight - 64, label = "Load Score", labelColor = { default = { 1, 1, 1 }, over = { 0, 0, 0 } }, fontSize = 32, font = "Pacifico", onEvent = loadScore }) local gameOverbg = display.newImageRect( "bg.png", \_W,\_H) gameOverbg.x = \_W \* 0.5 gameOverbg.y = \_H \* 0.5 group:insert(gameOverbg ) local gameOverboard = display.newImageRect( "gameoverboard.png", 357,187) gameOverboard.x = \_W \* 0.5 gameOverboard.y = \_H \* 0.5 group:insert(gameOverboard) local gameOver = display.newImageRect( "gameover.png", 168,52) gameOver.x = \_W \* 0.4 gameOver.y = \_H \* 0.37 group:insert(gameOver) highscore = display.newImageRect( "highscore.png", 168,52) highscore.x = \_W \* 0.45 highscore.y = \_H \* 0.52 group:insert(gameOver) local replayButton = widget.newButton { width = 130, height = 42, defaultFile = "replayButton.png", overFile = "replayButtonover.png", label = "", onRelease = function(event) group:removeSelf(); main() ;removereset(); end } group:insert(replayButton) replayButton.x = \_W \* 0.62 replayButton.y = \_H \* 0.64 score.save() return group end
and I’ve got the score.lua from the tutorial
also I’m having to press the load.score button to make the score show on the replay screen , hopefully its an easy fix :)